Ledge Grab in Unity
Now that the Player can run and perform a running jump, we would like it to grab a ledge if it could reach it. First, we need to get a hanging idle animation which again we can get from Mixamo.com:
Next, import to Unity. Ensure that the rig is Humanoid and that the animation is looping.
To know if the Player’s hands could reach a ledge we need to create a collider where it could reach the hands. For that we will create a cube and resize it near the Player’s hands.
As you can see above, for testing, I just use a trigger for Hanging to be able to position the cube which is renamed to Ledge_Grab_Checker properly.
I’ve also converted the Player into a Prefab.
I used a trigger to transition to Hanging Idle animation.
Next, we need to get another collider called Ledge_Grab that should collide with Ledge_Grab_Checker determine if the Player can hang or not.
If it is true, we need to place the Player in the Hanging position at the spot where the hands are touching the ledge which in my case is called a Ledge_Hang_Position.
In terms of coding the Ledge_Grab_Checker has the OnTriggerEnter method to check if it has caught a Ledge_Grab based on its tag.
The LedgeGrab on the other hand has the Transform for the position where the Player will hang.
Then finally, it will call Player.GrabLedge() to stop gravity and place the Player in the hanging idle animation at the Ledge Hang Position.
Notice, that I used Icons for each GameObject so it can easily be seen from the Game Editor whether the collider is selected or not. After testing, you can disable the mesh for Ledge_Grab_Checker and Ledge_Grab.
Now, grabbing a ledge and hanging on it works!
In hindsight, since you can only trigger hanging from a running jump, then the trigger should be placed on the Running Jump state.