Day 99 — Falling Idle Animation
Hey and welcome!
Next on the bucket list before reaching 100 days of articles is an idle falling animation that will play after a jump animation or if the player accidentally runs off the edge of a platform.
The first thing to do as alway is to find an animation you like and import it into Unity.
There aren’t really any strict requirements for this one. You just need something that will loop naturally without any additional animations added to it like a landing animation.
Once that’s in Unity it’s time to add it to the animator which is going to bring a new level of complexity to it. To start with, make two transitions going back and forth to your falling idle animation from your running animation and then create a transition that goes from your jump to your falling idle. Add one last transition going from your falling idle to your regular idle animation so that you have something looking like this:
You’ll see that I’ve highlighted the transition from jumping to falling here. Make sure that you have the exit time checked for this one and the rest of your new transitions without it checked. This makes it so that our jump animation will play in full before moving to the falling idle.
With that sorted out create a new bool parameter called Falling and add it to each of these new transitions as well as the transition from jumping to the regular idle. Make the condition true for the transitions from running to falling and jumping to falling and make the rest of them false. Lastly add a Speed greater than 0.1 condition to the transition heading from falling to running so that the player goes right back to running if they’re still moving.
It’s looking pretty wild now but that’s the last animation to add for this project, next bit now is to add in this code to the Player script:
if (_controller.isGrounded == true && _onLadder == false)
{
_anim.SetBool("Falling", false);
if (_isRolling == false)
{
if (_jumping == true)
{
_jumping = false;
_anim.SetBool("Jumping", _jumping);
_anim.SetBool("Falling", true);
}
}
}
else
{
_anim.SetBool("Falling", true);
}
This is an abridged version of the code again but you should be able to see that whenever the player is grounded we make sure that the Falling parameter form the animator is false. The times we make this parameter true is when the player isn’t grounded or when the jumping animation has finished.
Now this may work for you right out the box but if you have something like the below happening for you then there’s one more change we need to do:
What’s happening here is that the Falling parameter is constantly flickering from true to false which leads it to briefly play the falling animation while running on the ground. The reason it does this is because the isGrounded property in the animator controller is what’s flickering from true to false. It does this due to how we’re handling our y velocity in this line in the player script:
velocity.y -= gravity * Time.deltaTime;
Due to using Time.deltaTime the results of this code can actually vary depending on the frame rate that your computer can output! At higher frame rates this causes the y velocity to flicker to 0 which means our player comes off the floor briefly making the isGrounded property false.
If you are coming across this there are two ways to fix it. The first is a bit crude but you could ramp up the gravity until this stops happening and adjust the jump height variable accordingly. The better fix though is to edit our line from before:
velocity.y -= gravity * Time.fixedDeltaTime;
This makes the part of the code run at a fixed interval instead of trying to execute it as fast as it can. The results end up being what we’re looking for:
That’s looking pretty good I’d say! Next time is the day 100 of releasing daily articles and will also be the last article covering this particular project We’ll finish off strong by implementing a way for the camera to follow the player consistently as there’s an issue with how I’m doing it currently.