2D Player Animation (part 2)
Similar to how we created our Idle animation for our player, we will be following the same steps to add our running animation. We will also be scripting when and how to toggle the animation to play and when to switch back to idle when the player isn’t moving.
Objective: Add a Running Animation and adjust the script so you can toggle between the Idle and Running animation.
Player Running Animation Prep:
Instead of creating an brand new animator we are going add a new clip to the existing Animator. In the drop down menu within the top left Animation window we can select “Create New Clip”.
Once you save the file in the desired folder, you will be greeted with a new timeline that is linked to the same Idle Animation one. In the the Animator window you will see a new state that has been added. Right click and select “Make Transition” to create a link between these two states. You want one transition from Idle > Running, and another transition from Running > Idle.
Next we are going to slice and add in the individual images into the Animation.
Finally, we are going to set up the conditions to toggle the Animation. We can set the conditions and parameters within the Animator window. We are going to add an new Parameter of a Float Value which we will name “Move”.
Next we are going to add the parameters as a Condition within the Transitions. so When Idle > Running we want the Move Float value to be Greater than 0.1, and vice versa for the Running > Idle, where we want the opposite to happen.
Player Animation Script:
For this project we are going to separate the player script and the player animation script. The player Animation script will solely focus on animation and toggling the correct animation that the main Player script requires. This will keep the scripts organized and clean.
In the player animation script, we first need to have access to the Animator within the child sprite object.
Then we are going to create a public method which the main player script can get access to. We will name this public method “Move”. Since in the conditions we set the parameter to have a Float value, we will need to link that float value from the “Horizontal movement” that the player controls and apply it to the Animator. Therefore whenever the player moves, the values of movement are applied to both the player and also the player animation script.
In the player script, we are going to first gain access to the “Player Animation script” so we can get control over the public method created in the Player Animation script.
Then in the movement method we are going to toggle the public method in the player animation script while also passing along the float value gained from the Player “Horizontal Movement”.
Once that is completed, back in Unity you will noticed that the player now toggles the running animation while moving. If you find that there is a lag between the animation transitions, make sure on the Animator window that “Has Exit Time” has been unchecked and that the Transition Duration is also set to Zero.