Scripting Action Animations in Unity
Run, Jump, or Stand Around and Look Cool
The next few tasks in building my 2.5D platformer will involve adding animations to my player. We won’t get through all the animations above in this article, but that’s the general course of things to come.
We’ll start simply, with:
Idling
This animation doesn’t have any special code or triggers. It is the first animation we load, and it’s where we go when no other character state is called for. From Idle, it is possible to transition to the Ladder Idle animation (more on that later) and the Fast Run animation. Let’s focus on that last one. Here’s the transition from Idle to Fast Run:
You can see this triggers the run animation whenever the Speed float parameter is less than 0.1. We’ll look more closely at that in the next section.
Fast Run
The fast run animation is triggered via the Speed float parameter of the Player’s Animator. In my CalculateMovement() method there is a line that will update the Speed parameter whenever the player’s horizontal input changes.
When horizontal input is received, Speed goes higher than 0.1 and the run animation plays. If it falls back down below 0.1, then we return to the idle animation.
Running Jump
If I press the spacebar while running, I’ll execute a jump. This triggers the Jump trigger parameter. The Animation plays until the character controller is Grounded, at which point it transitions either to Idle or Fast Run.
The code for signaling the animations is added to my CalculateMovement() method:
Now the animator knows whether we’re touching the ground, and whether we left the ground because of a jump. We’ll rely on that ground detection later when we add a jumping down animation.
That does it for today’s additions. Tomorrow I’ll add ledge grabbing to the player’s feature set.