2D Mobile Game: Player Jump Animation
In this article, we will cover how to set up the Animator to transition from Idle to Jump and call it using code.
We already uploaded the Jump animation into the project and we are looking at the Animator tree to determine how we will transition from Idle animation to Jump animation. The type of parameter we need to trigger the transition is a Bool because jumping is either true or false. This Bool parameter, Jumping, when true will transition from Idle to Jump and when False will transition from Jump back to Idle
In transition from and to Idle and Run, we want to much sure Has Exit Time is false, which will allow the animation to instantly transition. Also, we want to set the Transition Duration to 0, so there is no blending between the two animations.
Lastly, make sure in the Animation setting Loop Time is false, so the Jump animation will only run once.
We have a Player animation script that is handling all of the player’s animations, but it will need to be able to communicate with the Player script we is taking care of the player mechanics. A public method is needed (Jump), so it can be called from the Player script. Since the animation transition is a bool, we are going to use a bool parameter (jumping). We access the Animator handle, set the “Jumping” animation transition bool to the bool parameter of the method (jumping).
In the Player script, when the player is jumping we access the Player Animation handle, call the Jump method, and pass in true. This set the transition parameter (Jumping) to true, so the jump animation will play.
We will need to do the same, but this time, when the player is grounded we are going to call the Jump method and pass in false. This set the transition parameter (Jumping) to false, so the jump animation will stop playing and it will revert back to the Idle animation.