2D Movement Animations in Unity
When you get the player moving you will want to add animations. To do this open the animation window and dock it next to the console.
With your sprite selected hit, create a new animation for your sprite. This will create an animation and add an animator to your sprite.
Now make sure to splice your sprites correctly, hit record in the animation window, and drag the sprites in.
Double click the animator controller and it will pop up for you. Set up your animator to have the idle transition to and from run depending on a speed parameter.
Create a script for your animations and add it to the game object your sprite is under.
Get a reference to the animator and create a method that takes in a float value and sets the float value for the speed parameter for the animations.
In the player script, get a reference to the player animation script. After setting the velocity of the player pass the horizontal movement into the move method.
This will get the animation transitioning between idle and running.
The last thing to do is to flip the sprite according to the direction the player is moving. To do this, get a reference to the player’s sprite renderer. Create a method in the player animation script that will take a float value. Depending on the value the player sprite will be flipped along the x-axis.
This will get the player moving left and right and have the sprite facing the right way.