8 Directional Player Movement in Unity 2D / 2021 (Tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to my channel in this video i'll be showing you how to put together an eight directional player movement script and animations for your top down game as you can see i have a blank unity project and i'm gonna go ahead and set it up with just a few uh things that i like to have set beforehand so i'll be right back all right so as you can see i just added my basic folder structure that i always like to have i added our player art and i added a script i made beforehand for the eight directional movement so first thing you'll want to do is create your player game object you'll also want to go ahead and add a sprite for your player now i'll go into our player art mine happens to be 32 pixels per unit it is multiple images and it is pixel art so i will choose filter mode point no filter and i'm going to turn the compression to none and apply that then i'll go to the sprite editor and i want them to have the pivot at the bottom of the sprite automatic's fine for me so i'll go ahead and slice up my images and click apply now i can go to my sprite and choose one of my player images now we can go over to our player game object and i am going to add my player top down script for movement in eight directions and i'll go ahead and open this and we can go over it all right this script is not very long so if you wanted to freeze the video right now you could take a look at the whole script and make sure you understand it and if you wanted to copy it go right ahead but i'll go over it here in a second all right so the first component we have here is a rigid body 2d second one is an animator a float for our movement speed an and a vector 2 for our movement input in our wake function we're going to be grabbing the rigid body component as well as the animator component for the game object this script is attached to in our update function we have a move function and an animate function for the move function i store the horizontal and vertical inputs from get axis raw and i do a check to see if they are both equal to zero and then i stop the player movement and i return to the update function and the reason i do a new vector 2 here for 0 0 is one to stop the movement but then also make sure that movement input does not get changed so then later on our animations don't change but if these inputs are not zero then we would change our input to whatever they are so a new vector 2 with the horizontal and vertical positions or horizontal and vertical inputs our rigid body velocity will then be set to our movement input multiplied by our movement speed multiplied by time dot fixed delta time and this makes sure that the animations and movement speed and everything runs at the proper time on all devices uh and our final function here is animate so for our animator we're gonna have to set floats for our movement x and movement y inputs so we're just setting the movement input dot x and movement input dot y for movement x and movement y and with that all said let's go back to unity and make sure we can set everything up so it works with this script so first thing we need is a rigid body 2d i'm going to turn off gravity set collision detection to continuous freeze the z rotation so if we bump into anything we're not rotating our player and i want to set our movement speed to something like 150 now i just need an animator and i'll right click over here add the animation tab and i'm going to go ahead and create our first animation which i will call player up click the record button go to the sprite and choose our player looking up sprite and now our player looks up whenever this animation is played so i'm going to go ahead and do the same thing but for all of the other animations we need so the seven other animations for eight directional movement all right and now i have all eight of our player animations so i'm going to go ahead and open the animator window and we can go ahead and clear out all of these animation clips here because we're going to be using a blend tree so i'm going to call this blend tree eight directional movement and now i'll select our blend tree here change it to simple 2d directional movement and i'm going to go ahead and select our parameters and i will add a new float called movement x and a float called movement y and set our parameters to movement x and movement y and i will get get rid of this blend component now that we just have our movement x and movement y let's go over here to movement or to motion and we can add a new motion field and we will actually want to add eight of those so i'll go ahead and do that so now that we have eight of our motion fields we'll want to spread them out evenly so i'll go ahead and set this first one at one zero then one one then zero one then let's see negative one one negative one zero negative one negative one one negative one and zero negative one so now you can see all these dots are evenly spaced from the red dot and we can go ahead and add the appropriate animation here for the correlating position so for example right here with the 1 and the x and the 0 and the y the player should be moving to the right so go ahead and add our player right animation to that motion for our next one it looks like the player would be moving up and right so i'll go ahead and add that in animation and i'll go ahead and fill the rest of these out too all right and with all of our animations added in you can see our blend trees filled out nicely here and everything is evenly spaced so we should be able to transition between each of these pretty seamlessly and as you can see down here in our animate function this is where we're setting our input so we're getting our input from our horizontal and vertical get axis raw inputs and then we're setting our movement x and movement y parameters based on those inputs so that's what feeds into here and this is what determines which animation should play so now if i click play we can see how it's working so far and if you move around you can see you do have a directional movement but it doesn't look so good just because it's just swapping out a sprite so what i like to do is i typically add another layer on top of it so i'm going to go ahead and change this name to the movement layer and i'll add a new layer called bounce move that on top this one has a w this layer at the top always has a weight of one so i'm also going to make our movement have a weight of one so that both of these animation layers play and now i can go to our player and create a new clip a new animation and i'm just gonna call this bounce and i can click on our record button go to the sprite i'll go to this 30 second mark or the 30 frame mark and i'm going to make our y squish down to 0.9 scale out our x to 1.1 so it's a little wider you can see what we're doing here adding a little squish then over here at 1 i'll go ahead and reset it so it looks normal and i want it to be a little faster so i'll put it at 45. now if we watch we can see we have a nice little bouncing idle animation so that looks nice and since it is on our bounce layer and our bounce layer goes directly into our bounce clip that should play over top of our animations for our movement so now we should have a little bit of a bounce in our step when we move our player around so let's take a look at that and just like that we got ourselves a little lively eight directional character now of course this process can get a little more complex with the more animation frames you want in there for the movement but this should give you a nice baseline on where to start if you liked this tutorial leave a like on the video and leave a comment down below for what i should do next and feel free to subscribe thank you very much bye
Info
Channel: MichaelsGameLab
Views: 634
Rating: undefined out of 5
Keywords: unity, unity 2d, top down, player movement, 8 direction player movement, game dev tutorial, tutorial, unity 2d tutorial, unity animation, blend trees, unity 2d blend tree, animation layers, Unity3D, Tutorial, Unity Tutorial, Beginner, Top Down Movement, TopDown Movement, Top Down, Movement, Player Controller, Character Controller, 8 direction, learn, how to, Move in unity, 2D Top Down Movement, game development, game, video game, roguelike, rpg, rigidbody, rigidbody2d, controls
Id: KG_XBc2a6-4
Channel Id: undefined
Length: 13min 7sec (787 seconds)
Published: Sun Aug 15 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.