Climbing Up from the Ledge in Unity!
Objective: Download and Implement a climb up animation!
We have given our player the ability to grab a ledge. In this tutorial we are going to give the player an input command to climb up to the platform we are hanging from.
To begin, we need an animation. Navigate to Mixamo and search for ledge and select this climbing animation.
Add this using the same methods as before, on the inspector select the bake into pose for all three options. Make a transition from your hanging animation to the climbing animation.
Since we are not working with root animations our animations does not move the character. This means when the climbing animation is performed the collider is still hanging on the edge.
The trick to ledge climbing is “teleporting” our player to to the standing position. One way to control this is to add a behavior script to our animation clip, climbing. Select the clip in the animation view and in the inspector click add behavior > new script.
Inside the animation behavior script you have a list of methods that are predefined for you. These are call backs that are automatically called during the animation that this is attached to. When the animation is finished, the On State Exit method is called automatically.
remove the comments on the on state exit method. On State Exit does not get called until the animation moves to another animation. play the editor until the player is stuck in a kneel after climbing up. Drag in a prefab of the player and place the duplicate on top of the kneeling player. Match the feet and write down the position’s X, Y, and Z values.
We want the ledge class to know about the stand position. Open the ledge class and create a new vector3 “_standPos” and enter the values your wrote down into the inspector.
When we call the grab ledge method we can store a reference to the current ledge we are on by using a parameter and a variable.
On the ledge class you will now pass in the hand position as well as the ledge.
We want to keep the stand position private on the ledge class. so to access the values of stand position create a helper method to retrieve it. Create a return method, when called upon this method will return the value of the stand pos.
The player can access this when the ledge is assigned. Create a method for when the climb up is completed. Assign the player position to the stand position, turn the animation bool for the grab ledge to be false and re-enable the controller.
We don’t want to create variables on the animation script however, we have access to the animator.
From that we can find the player component. Since the object that has the animator is child to an object, and that object is child to the player I need to access the parent of the parent. (That’s why there is parent.parent)
When the animation is finished we want to call the Climb Complete method.
Make a transition from Climb up to idle and set the transition duration to zero.
We have a successful climbing animation. Don’t be afraid to experiment further with your player’s position and the animations. I’ll see you in the next tutorial!