Hang In There!- Game Dev Series 89
Objective: create ledge grabbing animation.
Ledge grabbing is a key action to performance the perfect timing of player control.
In this article, we would create a ledge grabbing animation transition.
The progress of ledge grabbing could be in 3 parts:
- Setting the grabbing position of Player
- Setting the ledge detecting area
- Process grabbing animation when trigger the ledge detector
First, we need a hanging animation to do this part.
Select a proper hanging animation from Mixamo and import it to Player. Then we can enable it with a new bool parameter in Animator to hold the animation. This parameter would be the condition in the transition from Jumping to Handing animation.
With the state of animation, we can add a cube to Player. Rename it as “Ledge_Grab_Checker”. Then adjust the size as a handle for Player to grab.
When disable play mode, the cube should be gone. Make sure keep all the values of transform to create the cube before disable play mode.
And we also need to create a tab to this checker. It would be easy in the upcoming process.
Once it done, make the grab checker as trigger collider. Then we can create the ledge.
We need to set a detecting area to know if Player is touching the ledge.
Create a cube object and adjust the size, then remove mesh renderer to keep the collider.
With this trigger collider, we can use it to detect if the ledge grab checker of Player has collided with. Add a rigidbody component and disable gravity, then create a script to deal with colliding.
Insides this script, use OnTriggerEnter to check if this has collided with Ledge_Grab_Checker.
The effect we want is when Player touched the ledge, it should grabbing on it and hold it. However, the gravity of CharacterController would not allow this to happen. To do so, we need to disable CharacterController after we touched the ledge and performance the animation of grabbing.
We can create a public method in Player script and call this method when collided.
Now we don’t need to worry about gravity, let’s add another line in it to enable the animation transition.
With this method, we can call it from Ledge_Checker. Complete the OnTriggerEnter in Ledge script with getting the reference of Player.
Play and test it, you should see Player into grabbing pose when touched the ledge.
When we grabbing on the ledge, you would notice that we are not on the right position of ledge. That is because we disable ChracterController right after we collided with Ledge_Checker. To make Player snapped to the proper position, we need the Vector3 value and use it in scripts.
As we do when we imported the grabbing animation, make the Player moving to a position that looks like he really hanging on the ledge.
When we get a proper position, copy the value of transform and use it in Ledge script.
With this variable, input the value of that position in editor.
Then use this variable to the method that we called from Player.
And use the Vector3 as a new position in GrabLedge() method.
That’s it! Play it again and Player should hanging on the position that looks like grabbing on ledge.