Roblox Studio - 8 Direction Movement System - Scripting Tutorial
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
đź 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.
đ 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.
đ 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.
đ§ 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
đĄSkinned mesh character
đĄLocal script
đĄAnimations
đĄAnimation IDs
đĄBindable event
đĄUser input service
đĄController
đĄRun service
đĄAnimator
đĄShift lock
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
hello everyone in this tutorial we will
be covering the topic of eight
directional movement in Roblox today
we'll be achieving the type of movement
you can see on screen right now as you
might have noticed I am using a skinned
mesh character don't worry though you
can still use roblox's R six or R15
characters and get the same results if
you want to use skinned mesh characters
check out my other videos those can
guide you through setting one up for
your project we'll start off with
creating a new local script in the
starter character scripts folder I'll
name it animate so that it will replace
the default Roblox animate script you
can use a different name
though before I script anything I've got
to set up the animations I will use for
the eight directional movement system
for this movement system you will need
eight animations each covering a
different direction of movement and one
additional idle
animation I will make a duplicate of my
starter character and put it inside the
workspace and then I'll set up and
publish all my
animations if you need help with setting
up your own skinned mesh character and
the animations for it I'll leave a link
in the description to my previous
tutorial that covers this
topic all right now that I've published
my animations we can proceed by deleting
this character from the workspace and
removing the saved animations we won't
need those anymore next we'll make a
folder inside our animate script and
I'll name it
animations inside that folder I will add
nine animation objects these are what's
going to hold my nine animations I need
now I'll rename them corresponding to
whichever direction they're going to
represent you can pick different names
if you want to just make sure you're
consistent with the naming of them
although I do recommend following my
naming convention because it will affect
the scripts as well next we have to set
the animation IDs inside each animation
object we do this following the names of
the object and the animation we
published so idle ID goes into idle
forward ID goes into forward and so on
now that our animations are ready we'll
create another local script and I'll
rename it to input this is going to
check our key
presses inside that script I'll make a
new folder and name it events this will
hold our events that will pass
information between these two Scripts
next we'll add a bindable event inside
that folder rename it to movement input
changed we need another bindable event
we'll call this one shift lock
changed all right now we can get to
scripting first we will add a reference
to the user input service with it we can
get notified of key presses and other
player controls
then we'll create a function called
input began this will process key
presses the parameters are input and
game
processed we add this function as a
listener to the input began event of the
user input
service next we do conditional
statements to check if the player uses
keyboard and to check if the key pressed
is left shift which is my camera lock
key or shift lock key
add a reference to the shift lock
changed
event and now we just call fire on the
event now we need a reference to our
player we can use local player for this
since it's a local script
we also need a reference to the
controller of the player module we can
get it like
this the controller is used for reading
the current WD
input also we'll need to register the
Run
service I'll comment the code a bit just
to make it easier to
read next we'll make a function that's
going to run every frame I like to call
it update the parameter is Delta time
this is the time in seconds since the
last
frame add the update function as a
listener to the heartbeat event of the
Run
service now I'll make a function that's
going to process the player's movement
input the parameter is going to be a
vector 3 called movement input
we need to Cache the player's current
movement input this variable will only
change when there's a difference between
what the player is pressing and what's
been cach to this variable we initialize
this with a value of vector 3 which just
means there's no
input I'll quickly rename this function
as I think the name is more fitting and
we just call that function every frame
in the update function and we'll send
the controller's current move Vector as
the parameter this is our WD input
inside our read movement input function
we'll add a conditional statement to
check if the controller input is
different to the last cached movement
input and if there is a difference we'll
just save the controller move Vector as
the current movement
input we also need a reference to our
movement input changed event
once we have a reference to the event we
just fire it and pass the current
movement input as the
parameter all right for now we're done
with the input script so we can close
that and let's go into our animate
script we start off with caching our
player and the
character for the character we can just
use script parent since this script will
always be a child of the player's
character add a reference to our
characters humanoid using the find first
child
method and we need the animator which is
a child of our
humanoid we'll cache a variable called
shift lock enabled and default it to
false this will track if our player has
the camera lock enabled add another
variable called current movement
Direction default it to Vector
30 structure the code a
bit get references to our input scripts
events
now we need a function that's going to
listen to the shift lock change
event we add it as a listener to the
event this function doesn't have a
parameter that's because we're tracking
whether shift lock is enabled in the
script so we just inverse the current
value like
this we'll do the same for the movement
input change event with the difference
being the parameter this function has
one and it's a vector 3 value and also
it uses the other
event then we just cache our current
movement direction as the last player
movement
input we can do a quick test just to
make sure everything works
you can see in the output the movement
Direction Change is recorded and so is
toggling shift
lock great next we'll load all our
animations to our
animator Define a function call it load
movement animation
make a call to the function right after
its definition this makes sure all the
animations are pre-loaded into our
animator we'll make a couple of null
checks just to make sure everything's
ready for pre-loading the animations you
can also add debug print statements here
just to make it easier to track issues
however I won't do that because the
output tells you which line the error
was in the stack
after the null checks we'll add a for
Loop we use the loop to iterate over the
animations inside our animations
folder before we process the animations
we need a table to Cache them inside of
I'll call this table loaded movement
animations defaulted as empty with the
curly
brackets now we'll add each animation
inside the for loop with the key being
the animation's name and the value being
being the animation itself but loaded
into the animator like
this let's remove this print statement
as we don't need it anymore we need to
figure out what each of our individual
inputs prints in our output box if we go
forward we can see the output is 0 0us
one this is our Cardinal forward
Direction and we need a dictionary with
each of these eight directions and the
null Vector 0 0 0 these vectors are
going to be the T's to our dictionary
and the values will be the name of each
animation that represents moving in that
direction back to the script and we can
remove this print
statement I'll Define the dictionary
right now you can pause the video once
it's done and just copy
it the key values have to be in square
brackets this means that the key is an
object value and not a string value
you also have to make sure that you put
the name values in the dictionary
exactly as your animations are named in
the animations folder this is very
important once we have the dictionary
ready we'll use it to compare the
current movement Direction with its
corresponding animation name value and
we'll use that name to play the required
animation now we need to Cache Which
movement animation is currently playing
we default it to
[Music]
nil let's define a new function it will
be responsible for playing the right
animation we make a call to this
function whenever our input changes so
in my case it's right after caching
shift lock input and movement input
changes inside the function we'll make a
for Loop and iterate our newly made
movement directions
dictionary I will be the keys of our
dictionary and V will be the value
paired with that
key first we check if our current
movement direction is equal to the key
and if it is this is the animation we
should be
playing next we check if there is an
animation currently playing and if there
is we stop that
animation after that we set the current
movement animation to the animation from
the animation dictionary that has the
key same as our movement dictionary
value
V and we can just play that
animation I also believe my animations
are a bit slow so I'll adjust the speed
right now you have to do this after the
animation was started
let's do a quick test now there's a few
issues first issue is once the player
loads the idle animation doesn't start
the second issue is that while shift
lock is not turned on our player moves
funny instead of just walking forward
but if we turn on shift lock we can see
everything works perfectly
now so let's stop the test and fix those
issues first let's take care of the idle
animation loading on start we'll make a
single call to the movement animation
change function this will load the idle
animation at the start since our
movement input is Vector 3 0 in the
beginning to fix the second issue we'll
go into this function and at the top
make a conditional statement that checks
if shift lock is enabled we'll also add
an else statement
this code works perfectly when shift
lock is engaged so we'll just copy that
into the first part of the conditional
statement in the else section we'll add
a check to see if our current movement
direction is different to zero we also
add an lse statement within this section
we'll do a check for our current
movement animation that's playing and if
there is one we stop
it because of the conditional statement
we know inside this section that our
movement input is Vector 30 0 so we have
to play the idle animation we'll also
adjust the
speed next we'll go to the first section
of the conditional statement inside here
we know the player is trying to move
because the movement input is not a
vector 3 0 since shift lock isn't
enabled the eight directional movement
shouldn't work either so we'll override
the current movement direction to be our
forward Direction
we'll check if the current movement
animation is different to the forward
animation if it is we'll stop that
animation and force play the forward
animation we also adjust its
speed let's do a quick test
and there we go you now have an eight
directional movement system you can
expand upon it as you see
fit thanks for watching hope you enjoyed
it and I'll see you in the next video
Voir Plus de Vidéos Connexes
How To Make a Topdown RPG in Godot 4: Adding The Player (Part 1)
Pygame Movement - Pygame RPG Tutorial #2
How to make a Roblox JOJO game #2 | Making Stand Combat
FlipaClip â INTERPOLATION in ANIMATION (tutorial)
Start Your Game Creation Journey Today! (Godot beginner tutorial)
Juiced Up First Person Character Controller Tutorial - Godot 3D FPS
5.0 / 5 (0 votes)