Creating a ledge grab mechanic | Unity
A quick guide about how to create a ledge grab mechanic in Unity
Objective: Animate the 3D model of our player with Unity.
In the last post I covered how to implement a new animation for the player with Unity. Now, it’s time to create a ledge grab mechanic for our player in our platformer game.
To start, let’s take a look at the current animations that our player can perform:
- Normal jump
- Running jump
If you haven’t seen my last post, I recommend you to check it in case that you don’t know how to add new animations with Unity:
Implementing the ledge grab mechanic
Adding the ledge grab animation
Now, to start implementing the ledge grab mechanic, let’s add the animation to our animation states. The animation that we’re going to use was obtained from Mixamo (for free) and displays the model of our player in an idle state while hanging from something:
To add the animation, let’s drag its file into the Animator window and make the respective transitions coming from the jumping states. Then, in order to control those transitions, let’s create a new bool parameter that indicates when the player is grabbing the ledge:
Once created, select the respective transitions and:
- Disable the Has Exit Time property to avoid waiting for the jumping animation to end before passing to the ledge grab animation.
- Add the bool as a condition to execute the ledge grab animation.
Identifying the ledges
And now, to continue with the ledge grab mechanic, let’s create a new gameobject with a collider or a 3D cube inside our player gameobject. Once created, let’s modify the new gameobject to fit the hand of our animation like this:
This way the player will move the gameobject while moving and it will work to check if there’s a ledge to grab:
Don’t forget to select the new gameobject and:
- Enable the Is Trigger property from the collider.
- Create and assign a new tag to identify it easily.
Identifying the player’s identifier
Next, to identify in which moment we need to trigger the ledge grab animation, let’s create a new gameobject (with a box collider or a 3D cube) in the respective ledge:
Now, to identify the ledge and trigger the animation as expected, let’s modify its properties to meet our needs:
Then, let’s select the new gameobject and:
- Attach a rigidbody to be able to detect the collision with the ledge grab checker.
- Create and attach a new script to indicate how to handle the respective collision.
Now, let’s create a new gameobject to indicate the position of the player where the hands fit correctly while grabbing the desired ledge:
Next, let’s open the Ledge script and create a new variable to store the Transform component of the last new gameobject that indicates the player’s position to grab the ledge:
Once saved, let’s drag the gameobject into the script component through the inspector:
Then, in order to implement the ledge grab animation correctly, let’s open the Player script and:
- Create a new public method that receives the position to take when grabbing the ledge.
- Disable the character controller component of the player to avoid using gravity or control the player while grabbing the ledge.
- Set the respective bool to true from the animator component to indicate that the player should execute the ledge grab animation.
- Set the position of the player to be the parameter received.
Finally, in order to identify the collision, let’s return to the Ledge script and use the OnTriggerEnter method. Once in the method, let’s check if the tag from the collider belongs to the gameobject inside the player by using the CompareTag method. If that’s the case, let’s get the Player script component from the parent (the player) and call the method that we created above containing the position to set to the player:
And now, if we run the game in Unity, we’ll be able to see that the ledge grab animation and mechanic is working as expected when the collision occurs:
And that’s it, we created a ledge grab mechanic with Unity! :D. I’ll see you in the next post, where I’ll be showing how to work with animation events within the states of our player in Unity.