Creating a player walk animation
2D Mobile Game

Objective: Play player walk animation when walking and play idle animation when sitting still.

To create an animation for walk follow the same process as here.

Then double click on the controller, Sprite to open the Animator tab where we will be transitioning the player animations.

In the animator tab, create a new parameter of type float and give it a name Move(whatever you like)

Then right click on both animation and select the Make Transition and select the animation to transition to.

This is how it should look after the transition.

Now select the transition line from Idle to Walk and there create a condition which says walk greater than 0 which means when this condition meets, the animation will change from Idle to run.
Also make sure that the Has Exit time is set to false and Transition duration is 0 to make sure there is no delay in transition between both animations.

Do the same with Transition from Walk to Idle as well. Note that there is no is equal to condition so we set the condition to is less than 0.1.

Then create a new PlayerAnimation script and add it to the player gameobject and it will handle all the logic related to player animation.

Then inside the PlayerAnimation script, create a reference variable to the the Animator and then store the reference of Animator in child gameobject to that variable.

Then create a public method which takes a float value as a parameter and inside the function give the float parameter which we created earlier in the Animator tab the float value received in the function parameter.
This will determine the value of the Walk in Animator thus transitioning from Idle to Walk and vice versa.

Now create a reference variable to the PlayerAnimation script in the player script and assign it the script component.

Then after the horizontal input, provide the absolute value of horizontal input to the Walk function of PlayerAnimation script to enable animations.
We provide absolute value because when player walks left, it gets the input of -1 which basically means less than 0.1 so player will play idle animation instead of walk if we don’t give an absolute value to the Walk function.

And this is the final result.