Animating our Avatar (2.5D Platformer) (2) — Jump and Falling Animations
Building on the running and idle animations implemented in the previous article, let’s go and add a couple of jump animations to our avatar.
Objective: Implement a run-jump, idle jump, fall, and land animation to the player’s avatar.
1. Back in Mixamo, search for something along the lines of ‘running jump’ and see if one of them takes your fancy. Download this as an .fbx for Unity, drag it into your Assets, and repeat this step for ‘idle jump’, ‘falling’, and ‘landing’ animations.
2. Select these FBX files within the Unity Editor and take a look at the Inspector. Under ‘Rig’, change the type to ‘Humanoid’ and select apply. Then expand the file in your project, duplicate the animation clip itself, and then drag that over to an Animations folder. Select the animation clip and adjust the settings for it in the Inspector. In particular, ensure ‘Loop Time’ is set to false on animations we don’t want to automatically repeat (e.g. a jump animation) and set to true on those animations we do want to loop (e.g. a falling animation).There are a couple of other settings here we’ll need to adjust to get the desired result; I’ve found that the jump animations turn out better when we select ‘Bake into Pose’ for both the root transform position (Y) and rotation:
As for the falling and landing/falling-to-landing animations, I’ve found these worked best for me when none of these options were selected, so I’d recommend trying the same (but do feel free to play about with this to see which ends up yielding the best results!).
3. Open up your player avatar/model’s Animator Controller and drag these animation clips into it. Now we need to position and connect these logically: our ‘Idle’ will lead into ‘Idle Jump’ if the player jumps, then will either lead to ‘Falling’ when the jump animation ends (and the player hasn’t landed) or will lead straight into ‘Landing’ otherwise. With a transition/connection selected, consider whether it makes sense for ‘Has Exit Time’ to be enabled or not. For instance, I’d say it doesn’t make much sense for our jump animation to ever get cut off by another animation, but it might make sense for our falling animation to get cut off as soon as a condition is met. So for the former I’d have Exit Time set to true and for the latter I’d have it set to false.
4. For this to work we’ll need to create and appropriately assign some parameters to these transitions/connections. I’ve personally found a ‘Jumping’ and ‘Grounded’ boolean work a treat, although a trigger might also work (particularly for the jump). Use the plus icon in the Parameters tab in the Animator to create these parameters, then, with a transition selected, add a condition to it in the Inspector just as we did when adding the run and idle animations. It’s a bit tricky to communicate this through images, but some trial and error is also a perfectly valid way of setting up this sort of state machine 😉
5. With both the idle jump, fall, and land and the running jump, fall, and land logic set up in the Animator, all we need to do now is handle the code side of things. The only two things we need to worry about with this setup of us is appropriately setting our ‘Jumping’ boolean to true or false as well as our ‘Grounded’ boolean.
So wherever our logic is which says a player has just jumped, we’ll use Animatior.SetBool(“[boolean name]”, [true or false]) to set jumping to true. And within out ‘CharacterController.isGrounded’ if statement, we’ll need to set ‘Grounded’ to true or false appropriately (and where the player is currently grounded, ‘Jumping’ can also be set to false).
I’ll include a copy of this section of code below:
Head back over into Unity, let it compile, and see how things are looking. Admittedly this may well take a fair bit of tweaking just to make sure it’s working how you’d like it to work, but the results can be pretty satisfying when you see the player’s avatar steadily coming to life 😉