Roblox Studio - 8 Direction Movement System - Scripting Tutorial

The Alcatraz Syndrome
18 Jul 202416:48

Summary

TLDRThis tutorial guides viewers on implementing eight-directional movement in Roblox using a skinned mesh character. It covers creating and setting up animations for each direction and an idle state, then coding a local script for input detection and movement processing. The script listens for key presses and player movement, adjusting character animations accordingly. The video also addresses issues like idle animation loading and movement quirks without shift lock, providing a comprehensive walkthrough for Roblox game developers.

Takeaways

  • 😀 The tutorial covers eight-directional movement in Roblox using a skinned mesh character.
  • 🎮 It's possible to achieve the same movement with Roblox's R6 or R15 characters without a skinned mesh.
  • 📂 A new local script named 'animate' is created to replace the default Roblox animate script.
  • 🔁 Eight animations are required for different directions of movement, plus one idle animation.
  • 🔗 Animation IDs are set inside animation objects corresponding to their names and the published animations.
  • 📝 Two local scripts are created: 'animate' for handling animations and 'input' for monitoring key presses.
  • 🔑 The 'input' script uses the UserInputService to detect key presses and player controls.
  • 🔄 The 'update' function in the 'input' script processes player movement input every frame.
  • 🎭 The 'animate' script caches the player and character, and references the humanoid and animator.
  • 🔧 The script includes conditional checks for shift lock and movement direction to play the appropriate animation.

Q & A

  • What is the main topic of the tutorial?

    -The main topic of the tutorial is eight-directional movement in Roblox, specifically for a skinned mesh character.

  • Can you use non-skinned mesh characters for this movement system?

    -Yes, you can use Roblox's R6 or R15 characters to achieve the same eight-directional movement results.

  • What is the first step in creating the eight-directional movement system?

    -The first step is to create a new local script in the starter character scripts folder and name it 'animate'.

  • How many animations are required for the eight-directional movement system?

    -You will need eight animations for different directions of movement and one additional idle animation.

  • Where should the animations be placed within the script?

    -The animations should be placed inside a folder named 'animations' within the 'animate' script.

  • What is the purpose of the 'input' script mentioned in the tutorial?

    -The 'input' script is used to check key presses and other player controls for the eight-directional movement system.

  • What is the role of the 'BindableEvent' in the script?

    -The 'BindableEvent' is used to pass information between the 'animate' and 'input' scripts, specifically for movement input changes and shift lock changes.

  • How does the tutorial handle the camera lock key?

    -The tutorial uses a function called 'input began' to process key presses, including the camera lock key, which is checked with a conditional statement.

  • What is the purpose of the 'update' function in the 'input' script?

    -The 'update' function is responsible for processing the player's movement input every frame using the controller's current move vector.

  • How does the script determine which animation to play based on player movement?

    -The script uses a dictionary that maps movement directions to animation names and compares the current movement direction to determine which animation to play.

  • What issues are addressed in the testing phase of the tutorial?

    -The tutorial addresses issues like the idle animation not starting upon player load and the player moving funny without shift lock enabled.

  • How is the idle animation handled at the start of the game?

    -The idle animation is loaded at the start by making a single call to the 'movement animation change' function since the initial movement input is Vector3(0).

  • What is the purpose of the 'shift lock' feature in the movement system?

    -The 'shift lock' feature allows the player to lock the camera direction, affecting how the character moves in relation to the camera when the eight-directional movement is engaged.

Outlines

00:00

🎮 Introduction to 8-Directional Movement in Roblox

This video tutorial is focused on implementing an eight-directional movement system in Roblox, using a skinned mesh character. The instructor reassures viewers that the same results can be achieved with Roblox's default R6 or R15 characters. The process begins with creating a new local script named 'animate' in the starter character scripts folder, which will replace the default Roblox animate script. Eight animations are required, each for a different direction of movement, plus an additional idle animation. The instructor demonstrates duplicating the starter character to set up and publish the necessary animations, and then proceeds to delete the character and saved animations from the workspace. Next, a folder named 'animations' is created within the 'animate' script, and nine animation objects are added and named according to their respective directions. The animation IDs are set according to the names of the animation objects. The tutorial then moves on to creating another local script for handling player input, with a focus on key presses and camera lock functionality.

05:00

🔍 Setting Up Player Input and Movement Detection

The second paragraph delves into the scripting process for detecting player input. A local script named 'input' is created to monitor key presses, with a new folder 'events' established to facilitate communication between scripts. Two bindable events, 'movement input changed' and 'shift lock changed', are added to manage input changes. The script references the UserInputService to detect key presses and player controls. An 'input began' function is created to process key presses, which is then added as a listener to the UserInputService's 'input began' event. Conditional statements are used to check for keyboard input and to identify the left shift key as the camera lock key. The script also references the local player and the player's controller module to read the current movement input. The 'Run' service is registered, and a function called 'update' is created to run every frame, using the heartbeat event of the 'Run' service. A function to process player movement input is also defined, which caches the player's current movement input and checks for changes, firing the 'movement input changed' event when necessary.

10:04

🎭 Animating Movement with Preloaded Animations

The third paragraph focuses on the animation script setup. The script caches references to the player and the character's humanoid and animator. It also tracks whether the camera lock is enabled with a variable 'shift lock enabled'. The script listens to the 'shift lock changed' and 'movement input changed' events, caching the current movement direction and toggling the shift lock state accordingly. A function 'load movement animation' is defined to preload all animations into the animator. A dictionary is created to map movement directions to their corresponding animation names, ensuring that the correct animation is played based on the player's input. The script includes a conditional check for the current movement direction and stops any currently playing animation before playing the new one. The animation speed is adjusted, and a test is conducted to ensure that the animations play correctly with the shift lock engaged and that the idle animation is loaded on start.

15:05

🔧 Debugging and Finalizing the Movement System

The final paragraph addresses issues encountered during testing and provides solutions to refine the eight-directional movement system. The first issue is the idle animation not loading on start, which is resolved by calling the 'movement animation change' function to load the idle animation. The second issue involves the player moving incorrectly when shift lock is not enabled. To fix this, a conditional statement is added to check if shift lock is enabled; if not, the movement direction is overridden to the forward direction, and the forward animation is played. The script also includes an 'else' statement to handle the case when shift lock is not engaged, ensuring that the player moves correctly. A final test confirms that the movement system works as intended, and the tutorial concludes with a reminder to expand upon the system as needed.

Mindmap

Keywords

💡Eight-directional movement

Eight-directional movement refers to the ability of a character in a game to move in eight distinct directions, typically corresponding to the cardinal and intercardinal directions. In the video, this concept is central as the tutorial aims to teach viewers how to implement this type of movement in Roblox using a skinned mesh character. The script mentions setting up eight animations for each direction and one additional idle animation, which are crucial for achieving smooth and intuitive directional movement.

💡Skinned mesh character

A skinned mesh character in Roblox is a type of character model that uses a mesh and a skin to create a more detailed and customizable appearance compared to the default R6 or R15 characters. The video script mentions using a skinned mesh character for the eight-directional movement tutorial, indicating that while it's possible to use this type of character, it's not a requirement, and viewers can also apply the tutorial to Roblox's standard character models.

💡Local script

In Roblox, a local script is a type of script that can only be accessed by the client and is typically used for tasks such as handling user input, controlling character animations, and managing local game logic. The script mentions creating a new local script named 'animate' to replace the default Roblox animate script, which is a key step in customizing character movement and animations.

💡Animations

Animations in the context of the video refer to the visual movements of the character that correspond to different actions, such as walking or idling. The script outlines the need for eight specific animations for each direction of movement plus an idle animation. These animations are crucial for creating the visual feedback that players experience when they control the character's movement.

💡Animation IDs

Animation IDs are unique identifiers assigned to each animation in Roblox. In the script, setting the animation IDs is a necessary step to link the animation objects with the correct animations that have been published. This ensures that when the script references a particular animation, it corresponds to the correct visual movement.

💡Bindable event

A bindable event in Roblox is a type of event that can be subscribed to by other scripts, allowing for communication between different parts of a game. The script mentions creating bindable events for movement input changes and shift lock changes, which are used to synchronize the character's animations with the player's input.

💡User input service

The user input service in Roblox is a system service that provides information about the player's input, such as key presses and mouse movements. In the script, adding a reference to the user input service is essential for detecting when the player presses keys, which in turn triggers the character's movement in the specified direction.

💡Controller

In the context of the video, a controller refers to a component that manages the player's input for movement, typically in the form of 'WASD' or arrow keys. The script mentions using the controller to read the current movement input, which is then used to determine which animation should be played for the character.

💡Run service

The Run service in Roblox is responsible for managing the game's heartbeat, which is the regular update loop that occurs every frame. The script adds a function to be run every frame by subscribing it to the heartbeat event of the Run service. This is important for continuously updating the character's movement and animations in real-time.

💡Animator

An animator in Roblox is a component that manages the playing of animations on a character. The script caches a reference to the animator, which is used to load and play the animations corresponding to the character's movement direction. The animator is a key component in the process of animating the character based on player input.

💡Shift lock

Shift lock, as mentioned in the script, refers to a feature that locks the camera's orientation while allowing the character to move freely. This is implemented by detecting when the left shift key is pressed and adjusting the character's movement accordingly. The script includes a shift lock changed event to handle this functionality, which affects how the character's movement is processed.

Highlights

Introduction to eight-directional movement in Roblox using skinned mesh characters.

Compatibility of the movement system with Roblox's R6 or R15 characters.

Creation of a new local script named 'animate' to replace the default Roblox animate script.

Requirement of eight animations for different directions and one idle animation.

Duplicating the starter character and setting up animations within the workspace.

Publishing animations and removing the character from the workspace after setup.

Adding nine animation objects to the 'animations' folder and setting their IDs.

Creating an 'input' script to check key presses and player controls.

UsingBindableEvents to pass information between scripts.

Adding a reference to the UserInputService for key press notifications.

Creating a function to process key presses and checking for the shift lock key.

Referencing the player and the controller module for reading current movement input.

Defining a function to run every frame to process player's movement input.

Caching the player's current movement input and firing a 'movement input changed' event.

Caching the player and character references, and initializing shift lock tracking.

Loading all animations into the animator and defining a function for pre-loading.

Creating a dictionary to match movement directions with corresponding animation names.

Defining a function to play the correct animation based on the current movement direction.

Adjusting animation speed and testing the eight-directional movement system.

Fixing issues with idle animation loading and movement behavior without shift lock.

Final test and confirmation of the eight-directional movement system's functionality.

Conclusion and invitation to the next tutorial.

Transcripts

play00:01

hello everyone in this tutorial we will

play00:03

be covering the topic of eight

play00:05

directional movement in Roblox today

play00:07

we'll be achieving the type of movement

play00:09

you can see on screen right now as you

play00:11

might have noticed I am using a skinned

play00:13

mesh character don't worry though you

play00:16

can still use roblox's R six or R15

play00:19

characters and get the same results if

play00:21

you want to use skinned mesh characters

play00:23

check out my other videos those can

play00:25

guide you through setting one up for

play00:27

your project we'll start off with

play00:29

creating a new local script in the

play00:31

starter character scripts folder I'll

play00:33

name it animate so that it will replace

play00:35

the default Roblox animate script you

play00:38

can use a different name

play00:40

though before I script anything I've got

play00:43

to set up the animations I will use for

play00:45

the eight directional movement system

play00:48

for this movement system you will need

play00:50

eight animations each covering a

play00:52

different direction of movement and one

play00:55

additional idle

play00:56

animation I will make a duplicate of my

play00:59

starter character and put it inside the

play01:01

workspace and then I'll set up and

play01:03

publish all my

play01:09

animations if you need help with setting

play01:11

up your own skinned mesh character and

play01:13

the animations for it I'll leave a link

play01:16

in the description to my previous

play01:17

tutorial that covers this

play01:26

topic all right now that I've published

play01:29

my animations we can proceed by deleting

play01:32

this character from the workspace and

play01:34

removing the saved animations we won't

play01:37

need those anymore next we'll make a

play01:39

folder inside our animate script and

play01:42

I'll name it

play01:48

animations inside that folder I will add

play01:50

nine animation objects these are what's

play01:53

going to hold my nine animations I need

play02:02

now I'll rename them corresponding to

play02:04

whichever direction they're going to

play02:05

represent you can pick different names

play02:07

if you want to just make sure you're

play02:09

consistent with the naming of them

play02:11

although I do recommend following my

play02:13

naming convention because it will affect

play02:15

the scripts as well next we have to set

play02:18

the animation IDs inside each animation

play02:20

object we do this following the names of

play02:23

the object and the animation we

play02:24

published so idle ID goes into idle

play02:27

forward ID goes into forward and so on

play02:38

now that our animations are ready we'll

play02:40

create another local script and I'll

play02:43

rename it to input this is going to

play02:45

check our key

play02:47

presses inside that script I'll make a

play02:49

new folder and name it events this will

play02:53

hold our events that will pass

play02:54

information between these two Scripts

play03:00

next we'll add a bindable event inside

play03:02

that folder rename it to movement input

play03:08

changed we need another bindable event

play03:10

we'll call this one shift lock

play03:14

changed all right now we can get to

play03:19

scripting first we will add a reference

play03:21

to the user input service with it we can

play03:24

get notified of key presses and other

play03:26

player controls

play03:30

then we'll create a function called

play03:31

input began this will process key

play03:34

presses the parameters are input and

play03:36

game

play03:39

processed we add this function as a

play03:41

listener to the input began event of the

play03:44

user input

play03:48

service next we do conditional

play03:51

statements to check if the player uses

play03:53

keyboard and to check if the key pressed

play03:55

is left shift which is my camera lock

play03:58

key or shift lock key

play04:06

add a reference to the shift lock

play04:07

changed

play04:12

event and now we just call fire on the

play04:19

event now we need a reference to our

play04:21

player we can use local player for this

play04:24

since it's a local script

play04:30

we also need a reference to the

play04:31

controller of the player module we can

play04:32

get it like

play04:35

this the controller is used for reading

play04:37

the current WD

play04:40

input also we'll need to register the

play04:42

Run

play04:46

service I'll comment the code a bit just

play04:49

to make it easier to

play04:55

read next we'll make a function that's

play04:58

going to run every frame I like to call

play05:00

it update the parameter is Delta time

play05:04

this is the time in seconds since the

play05:06

last

play05:08

frame add the update function as a

play05:10

listener to the heartbeat event of the

play05:12

Run

play05:17

service now I'll make a function that's

play05:20

going to process the player's movement

play05:21

input the parameter is going to be a

play05:24

vector 3 called movement input

play05:29

we need to Cache the player's current

play05:31

movement input this variable will only

play05:33

change when there's a difference between

play05:35

what the player is pressing and what's

play05:37

been cach to this variable we initialize

play05:39

this with a value of vector 3 which just

play05:42

means there's no

play05:44

input I'll quickly rename this function

play05:47

as I think the name is more fitting and

play05:50

we just call that function every frame

play05:51

in the update function and we'll send

play05:54

the controller's current move Vector as

play05:55

the parameter this is our WD input

play06:02

inside our read movement input function

play06:04

we'll add a conditional statement to

play06:06

check if the controller input is

play06:08

different to the last cached movement

play06:10

input and if there is a difference we'll

play06:13

just save the controller move Vector as

play06:15

the current movement

play06:17

input we also need a reference to our

play06:19

movement input changed event

play06:30

once we have a reference to the event we

play06:32

just fire it and pass the current

play06:34

movement input as the

play06:39

parameter all right for now we're done

play06:42

with the input script so we can close

play06:44

that and let's go into our animate

play06:49

script we start off with caching our

play06:51

player and the

play06:56

character for the character we can just

play06:58

use script parent since this script will

play07:01

always be a child of the player's

play07:03

character add a reference to our

play07:05

characters humanoid using the find first

play07:07

child

play07:14

method and we need the animator which is

play07:17

a child of our

play07:22

humanoid we'll cache a variable called

play07:24

shift lock enabled and default it to

play07:27

false this will track if our player has

play07:29

the camera lock enabled add another

play07:31

variable called current movement

play07:33

Direction default it to Vector

play07:41

30 structure the code a

play07:48

bit get references to our input scripts

play07:50

events

play08:02

now we need a function that's going to

play08:04

listen to the shift lock change

play08:09

event we add it as a listener to the

play08:15

event this function doesn't have a

play08:18

parameter that's because we're tracking

play08:20

whether shift lock is enabled in the

play08:21

script so we just inverse the current

play08:23

value like

play08:26

this we'll do the same for the movement

play08:28

input change event with the difference

play08:31

being the parameter this function has

play08:33

one and it's a vector 3 value and also

play08:36

it uses the other

play08:38

event then we just cache our current

play08:40

movement direction as the last player

play08:42

movement

play08:53

input we can do a quick test just to

play08:56

make sure everything works

play09:05

you can see in the output the movement

play09:07

Direction Change is recorded and so is

play09:09

toggling shift

play09:19

lock great next we'll load all our

play09:22

animations to our

play09:26

animator Define a function call it load

play09:28

movement animation

play09:35

make a call to the function right after

play09:37

its definition this makes sure all the

play09:39

animations are pre-loaded into our

play09:42

animator we'll make a couple of null

play09:44

checks just to make sure everything's

play09:45

ready for pre-loading the animations you

play09:48

can also add debug print statements here

play09:50

just to make it easier to track issues

play09:53

however I won't do that because the

play09:54

output tells you which line the error

play09:56

was in the stack

play09:59

after the null checks we'll add a for

play10:04

Loop we use the loop to iterate over the

play10:07

animations inside our animations

play10:09

folder before we process the animations

play10:12

we need a table to Cache them inside of

play10:14

I'll call this table loaded movement

play10:16

animations defaulted as empty with the

play10:19

curly

play10:23

brackets now we'll add each animation

play10:25

inside the for loop with the key being

play10:27

the animation's name and the value being

play10:29

being the animation itself but loaded

play10:31

into the animator like

play10:37

this let's remove this print statement

play10:39

as we don't need it anymore we need to

play10:41

figure out what each of our individual

play10:43

inputs prints in our output box if we go

play10:46

forward we can see the output is 0 0us

play10:49

one this is our Cardinal forward

play10:51

Direction and we need a dictionary with

play10:53

each of these eight directions and the

play10:55

null Vector 0 0 0 these vectors are

play10:58

going to be the T's to our dictionary

play11:01

and the values will be the name of each

play11:02

animation that represents moving in that

play11:05

direction back to the script and we can

play11:07

remove this print

play11:10

statement I'll Define the dictionary

play11:12

right now you can pause the video once

play11:14

it's done and just copy

play11:19

it the key values have to be in square

play11:22

brackets this means that the key is an

play11:24

object value and not a string value

play11:30

you also have to make sure that you put

play11:31

the name values in the dictionary

play11:33

exactly as your animations are named in

play11:35

the animations folder this is very

play11:49

important once we have the dictionary

play11:51

ready we'll use it to compare the

play11:53

current movement Direction with its

play11:55

corresponding animation name value and

play11:57

we'll use that name to play the required

play12:02

animation now we need to Cache Which

play12:04

movement animation is currently playing

play12:06

we default it to

play12:07

[Music]

play12:16

nil let's define a new function it will

play12:19

be responsible for playing the right

play12:25

animation we make a call to this

play12:27

function whenever our input changes so

play12:30

in my case it's right after caching

play12:32

shift lock input and movement input

play12:37

changes inside the function we'll make a

play12:39

for Loop and iterate our newly made

play12:41

movement directions

play12:45

dictionary I will be the keys of our

play12:47

dictionary and V will be the value

play12:50

paired with that

play12:55

key first we check if our current

play12:58

movement direction is equal to the key

play13:00

and if it is this is the animation we

play13:02

should be

play13:03

playing next we check if there is an

play13:05

animation currently playing and if there

play13:07

is we stop that

play13:21

animation after that we set the current

play13:23

movement animation to the animation from

play13:25

the animation dictionary that has the

play13:27

key same as our movement dictionary

play13:29

value

play13:37

V and we can just play that

play13:44

animation I also believe my animations

play13:46

are a bit slow so I'll adjust the speed

play13:48

right now you have to do this after the

play13:50

animation was started

play14:01

let's do a quick test now there's a few

play14:03

issues first issue is once the player

play14:06

loads the idle animation doesn't start

play14:08

the second issue is that while shift

play14:11

lock is not turned on our player moves

play14:13

funny instead of just walking forward

play14:15

but if we turn on shift lock we can see

play14:17

everything works perfectly

play14:23

now so let's stop the test and fix those

play14:27

issues first let's take care of the idle

play14:29

animation loading on start we'll make a

play14:32

single call to the movement animation

play14:34

change function this will load the idle

play14:36

animation at the start since our

play14:38

movement input is Vector 3 0 in the

play14:48

beginning to fix the second issue we'll

play14:50

go into this function and at the top

play14:53

make a conditional statement that checks

play14:54

if shift lock is enabled we'll also add

play14:57

an else statement

play15:01

this code works perfectly when shift

play15:02

lock is engaged so we'll just copy that

play15:05

into the first part of the conditional

play15:08

statement in the else section we'll add

play15:10

a check to see if our current movement

play15:12

direction is different to zero we also

play15:14

add an lse statement within this section

play15:17

we'll do a check for our current

play15:18

movement animation that's playing and if

play15:20

there is one we stop

play15:26

it because of the conditional statement

play15:29

we know inside this section that our

play15:30

movement input is Vector 30 0 so we have

play15:33

to play the idle animation we'll also

play15:35

adjust the

play15:37

speed next we'll go to the first section

play15:40

of the conditional statement inside here

play15:42

we know the player is trying to move

play15:43

because the movement input is not a

play15:45

vector 3 0 since shift lock isn't

play15:48

enabled the eight directional movement

play15:50

shouldn't work either so we'll override

play15:52

the current movement direction to be our

play15:54

forward Direction

play16:03

we'll check if the current movement

play16:04

animation is different to the forward

play16:11

animation if it is we'll stop that

play16:13

animation and force play the forward

play16:15

animation we also adjust its

play16:25

speed let's do a quick test

play16:29

and there we go you now have an eight

play16:30

directional movement system you can

play16:32

expand upon it as you see

play16:37

fit thanks for watching hope you enjoyed

play16:40

it and I'll see you in the next video

Rate This

5.0 / 5 (0 votes)

関連タグ
RobloxAnimationCodingTutorialGame DevelopmentCharacter MovementSkinned MeshScripting8-DirectionalMovement System
英語で要約が必要ですか?