Platformer: Free Falling & Landing
Intro
In this article I will cover how to trigger a free falling and landing animation, where a certain Y position must be passed before playing the free falling animation. This will not use the Character Controller which can detect when it is grounded or not, since that is used for the regular jumping mechanic and animation.
The Code
We start off by declaring the following variables…
- _lastY = This float is going to hold the Y position of player each frame.
- _fallingThreshold = This float will be how far the player will need to fall before triggering the falling animation.
- _falling = This boolean will track whether the falling threshold was met
On the start we set the _lastY to the current Y position of the player.
We create a public method IsFalling and we will create local variable called distanceSinceLastFrame. This will take the _lastY position that was captured and subtract it from the current Y position. This will be multiplied by Time.deltaTime to make it the difference since the last Update was ran.
Then we reset the _lastY to the new current Y Position for the next frame.
We check to see if the distanceSinceLastFrame is less than the _fallingThreshold.
Lets log the distanceSinceLastFrame. We can use this to set the _fallingThreshold and how sensitive we want it to be. Since the _fallingThreshold has a SerializedField we can easily adjust this in the Inspector. I played around with this threshold for some time to find what worked for my game.
This will determine if the player is falling or not.
This is difference is always being checked in the Update in the IsFalling method. When _falling is true, we call the Falling Method and if we call Landed.
Lets look into both of those Methods.
There is a game object that is above the Player is being used to see if the they are close enough for a ledge grab. This some times is triggered when just falling in between two platforms, so we deactivate to prevent it from accidentally firing. Finally, we set the animation transition “Falling” true. This transition is placed from both running and jumping.
In the Landed Method, we reactivate the ledge grab checker, set the animation transition for Falling false and we set the Landed animation transition to true.
This transition to the Landing animation can only be made from the Falling animation. You can see the transition to Falling in Red, the transition to Landing in Yellow and the transition back to from Landing to either Idle or Running. These using the horizontal input, a float, to determine if the player is pressing the arrow keys when they have landed which will have them run or they are not, which will have them idle.
The Falling Animation has a behavior script that checks the IsFalling Method from the player when the animation is finished playing to check to see if the player is still falling or not. If they still falling we trigger the falling animation again and if not, we trigger the land animation.
The Conclusion
This code took a lot of playing around because the animation web has grown to be more complex and I was trigger animations that I didn’t want. I hope to continue to grow in the area of animations and learn how to simplify them, but for now, this works. Until next time…Cheers!