Flip 2D Character Unity - Animation Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome to another video in this video we're going to be looking at animations for the sprite that we sliced up in the last video we'll set up an animator to transition between two animations we'll add a player controller script to control the animator and we'll also flip the sprite so that you can reuse the run animation so that you can run left and right without having two separate animation okay so let's get to it the first thing i'm going to do is look at my player player has a single sprite on it right now it also has a rigid body 2d the important thing about this is that when you add the rigid body 2d we only want to move left and right and you may want to move up and down since this is a 2d game we're going to freeze the rotation at z so that we don't have any awkward problems i'm also going to set the gravity scale to zero so that when i press play the player doesn't just fall down okay so with that done and the player selected i'm going to go to the animation window if you can't find the animation window it's window animation animation got it now i'm going to click player and click create and i'm going to create a folder for this i'm going to create an animations folder and the first one i'm going to do is i'm going to create a player run so now that's created i'm going to drag this window up here just to make it a little bit easier for myself player run animation i know is across player 2 to player 8. drag them all in i'm going to click play so they can watch them as they play this is a little fast and looks a bit ridiculous so you can drag the end bar with everything selected and drag it out and that will slow the animation right down this allows you to play with it a little bit once i'm happy with this animation i can move on and start creating the player idle animation i'll click this drop down and click create new clip i'm going to rename it and call it player idle i'll select the three sprites here and drag them in in the exact same way as i did for the run cycle test out what it looks like i'll click play and here we are might be a bit slow again once i'm happy i can stop and move on now that we have the two animations we need to set up the transition between the two do that i'm going to go to the animator window you can get to the animator window in the same manner as the animation window so it's window animation and animator with the player selected i can see both the animations i created here in the animator window the player run animation was the first one i created so it has a transition to it from the entry point we don't want the player run animation to be the first so i'm going to right click on the player idle animation and set as layer default state this means that when the game starts and the entry point triggers we can move straight from the entry through to the player idle animation now i want to transition between the player idle and the player run so to do that i have to right click on the player idol and click make transition this gives me an arrow here i can click on the player run and now i've got a transition to the player run i'm going to do the same in the opposite direction because i want to be able to transition from idle to run and then from run back to idle at this point and we need to update those transitions so i'm going to click the arrow here so there's a few things we need to change first thing is i want to remove has exit time the reason i want to do that is because i don't want any blending unity will make an attempt to blend between the two animations and it can look a bit strange it can really work out well if you know what you're doing but for this project we're not going to do i'm going to remove that and then i need to set a condition so we only want the player to move from idle to run when the player is moving at a certain speed and to do that i want to add a parameter i want the player to have a speed parameter on his animator so that i can set that speed parameter and then adjust the animations based on that to do that i go over here to the animator window if you've got layers selected just click parameters click the plus click float and we'll call it speed now it's defaulted to zero when i click over here as x of time is still clicked off i can click to add a condition and i want to move from idle to run when the speed is greater than 0.01 and there we have it we're going to do the same in the opposite direction we're going to change the has exit time to remove it we're going to add a condition but this time i'm going to set speed to less than 0.01 and that's it i've saved it i'm gonna run and when the game starts instantly my character is into the player idle animate and test out the transitions by changing this value to three so the player is running zero and he's running okay it looks like that's working now that we have the player animations working we need to be able to control that from the player controller and to do that we're going to actually have to create a player controller i'm going to click on player close up all of these i'm going to create a script called player controller by default this gets created at the top level in the assets folder but i don't want that so i'm going to create a folder called scripts i'll move the player controller into there so now it's time to start writing some code i'll open the script if we're going to move the player then the first thing we're going to need is a movement speed i'm going to create a variable up here i'm also going to create a vector 2 to store the movement that the user inputs now that we have those variables the next thing to do is start taking the user input so the way we can get the user input is by using input.getaccess and we can use horizontal because we only want moving along the x-axis so left to right i'm going to create a new a vector 2 and use input dot get access horizontal for the x value and since i don't want to move up or down i'm going to set the y value to zero now that i have that i want to make sure that i get a consistent value i'm going to normalize it and when i normalize it it means i'm going to get a consistent value in the right direction but it's all but the length is limited to 1. then i'm going to store that in that movement variable i created earlier it's best practice not to actually move your player in the update function for that we should use fixed update what i'm going to do here is i'm going to check if the movement vector is equal to zero we don't want to move if it isn't set to something so if it is set to something we can actually do some movement we already have a movement vector but that movement vector is the direction not the movement so what we're going to do is we're going to get the x movement by doing movement dot x we'll multiply it by the movement speed then we'll multiply it by time dot delta time next i'm going to move it so what's this doing we're getting the current objects transform and then we're translating it in a direction the direction we want to translate it in we create a new vector three and we set the x value that we've calculated on the previous line and then because we don't want y to move up or down we just set that to zero and then we're done that means that we should have a player that's moving now the player moves but he's not animating let's get to that i'm going to switch back and we're going to start looking at the animation so to animate the player we're going to need access to the animator that controls the animation i'm going to create an animator variable up here and in the start function i'm going to get the animator component attached to this object then what we need to do is set that speed variable that we were talking about earlier that we created in the animator and to do that we're going to go to the update function we're going to use animator dot set float we're going to use the name that we created which was speed and then we're going to use the movement variable but we want to make sure that we get a positive value so i'm going to do math f dot absolute so i get the absolute value and then i'm going to use the movement i'm going to use the magnitude and multiply that by the movement speed that should set our values properly and we should have an animating player and there we go but now you're looking at this and thinking going right is perfect going left we need to flip that sprite and that's really easy i'm going to move back to the player controller script and i'm going to update the update function in here we need to figure out if the sprite should be flipped so i'm going to create a flipped variable a boolean and i'm going to set it to i'm going to set it to movement.x less than zero so this means that it will be true if the movement dot x value is less than zero so if we're moving left then it should be flipped so now that we've done that we need to rotate the sprite and to rotate the sprite all you have to do is set the rotation we're going to use a built-in function for it called paternian dot euler what that does is it rotates around the x y or z axises by the amount you provide so we are going to provide a new vector 3. we don't want to rotate around the x axis but we do want to rotate around the y so if it if it needs to be flipped then we're going to rotate 180 degrees on the y if not we won't rotate at all and nothing on the z so with that done we're going to have to make one more change we're going to have to change how the translation is done down in the fixed update currently the translation is done relative to the object's local axis but what we want is for it to translate based on the world coordinate system that will mean that left will always be left right will always be right because we're working on the world coordinate system not on our rotated local warden system so with that done we should have an animated sprite he's working so that's how to animate a player and flip the sprite hopefully you've got something useful out of that and if you did consider subscribing and i'll see you next time
Info
Channel: ShootingDux
Views: 14,012
Rating: undefined out of 5
Keywords: unity, gamedev, devlog, flip 2d character unity, 2d character animation unity, flip character unity 2d, flip player unity 2d, flip player animation unity 2d, unity sprite animation, flip sprite animation, flip 2d sprite animation, unity 2d character animation tutorial, how to flip your character in unity 2d, how to flip 2d character unity, how to flip 2d character in unity, unity 2d character movement animation, 2d character movement flip, flip character sprite unity
Id: pKZ7FIL2csU
Channel Id: undefined
Length: 12min 42sec (762 seconds)
Published: Sun Nov 28 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.