Add a jump animation to player character controller in Unity
Objective: add a jump animation and transition to player animation controller
In the previous article I went through how to add a running animation and how to transition between idle and running and back again. The jumping animation needs to be able to happen when the player is idle and also when the player is running.
Like before add in your jumping animation to the controller then add in the transition lines.
- from idle to jumping
- jumping to idle
- running to jumping
- jumping to running
Now that the transitions lines are made we can make the parameter that we’re going to need to change between the animations.
My parameter of jumping is a bool and is called IsJumping.
Now to set up the bools in the transitions.
Idle to jumping will have no exit time, no fixed duration and the condition is set to IsJumping is true. For the reverse: no exit time, no fixed duration and it will need two conditions. Remember we only want to go to idle if our speed is <0.1 and IsJumping is false.
Running to jumping and the return is more simple. From running to jumping we have the same setup for the exit and duration and the condition is set to the IsJumping = true. The return is the same but change true to false.
Now we go into the code.
I have created a private bool called _jumping that is initially set to false.
When the space bar is pressed, I then set the private bool to true and call the animator with the updated bool. In the next update, the if statement above will be called as jumping is true now, it will then immediately turn false and call the animator again.
To see the animator while the game is running have the animator window open and select the game object that has the animator controller component attached. You will then see the transitions between animations. If an animation is playing then there will be a blue line at the bottom of the animation clip.
And that is it for adding in the jumping animation. Come back for the next article where I will be going through adding in a double jump animation.