Climb up animation
Next step is climbing the ledge. We will chose an animation for climbing, and create logic. The transition from hanging to climbing will happen after pressing the ‘E’ key.
First we need to reset all the parameter for speed and jumping in the Animator Controller when we are in Hanging Idle animation.
Next we need climbing animation. Go to Mixamo and find the animation you like.
Download it and place the file into your project. Click on the file in Unity and change the Animation Type to Humanoid.
Duplicate the animation file by pressing Control + D, and put the animation in the Animator Controller of the Player. Bake all the positions.
Make a transition from Hanging Idle animation and Climbing animation.
Add a new Trigger parameter called “ClimbUp” and add it ass a condition for the transition. Set Has Exit Time to false.
Now we want to press the ‘E’ key to play climbing animation. Go to the Player script. We will create a new bool variable called _onLedge. This way our script will now when to check for the key press.
When we call the GrabLedge() method we will change this variable to true.
Now, we will check in the Update() method if the Player is on ledge. If he is, then we will check if the ‘E’ key was pressed.
Now, we need to move the position of our Player to the position of the ending of the climbing animation. Right now our Player only moved because of the animation. The Character Controller didn’t change its place.
Duplicate you Player and disable one of them. Then, make your Player climb. Pause the game, and enable the second Player. Move the second Player in the Idle state to the position you want your Player to be when he transition from Climbing to Idle. Write the the position of the second Player.
The position of the Player will be stored in the Ledge_Checker script. Open the Ledge script and add a new variable.
Find the Ledge_Checker in the Inspector and set the _standPose.
In the player script add a new variable called _activeLedge. This way our script will be modular.
Now, we will go to GrabLedge() method and pass in another parameter of type Ledge.
Now, when we called this method in the Ledge script, we need to pass an variable of type Ledge, which is “this”.
Create a new script in the Player script called CimbingUpComplete(). To snap the position of the Player we need to retrieve the _standPose variable from Ledge script. The thing is, this variable should stay private. To retrieve this variable we will create a new method in Ledge script, called GetStandPose(), that will return Vector3.
In the ClimbingUpComplete() we will call this method and set GrabLedge to false. We will also enable the Character Controller component.
Click on the Climbing state and add a new script to it called ClimbUpBehavior.
OnStateExit() method should be automatically called when the animation is finished. Uncomment it.
For this method to be called, we need to transition from Climbing animation to Idle. Let’s create this transition.
In the OnStateExit() method we need to access the Player script. If the payer variable is not null, we will call the ClimbingUpComplete() method.
The transition between the end of the Climbing animation and the Idle is not really smooth.
To fix it, we will add another animation between. Go to Mixamo and find Stand animation
Download it and add it to you project. Change the file to Humanoid, and duplicate the animation.
Add the animation to the Animator Controller, and add transition without conditions.
Transition between Climbing and Stand animation doesn’t have transition duration.
Transition between Stand and Idle is change to make the Player stand up faster.