Nerd For Tech
Published in

Nerd For Tech

Unity Guide

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.

Player mechanics

To start, let’s take a look at the current animations that our player can perform:

  • Idle
  • Walk
  • Run
  • 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:

Don’t forget to enable the Is Trigger property from the collider.

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:

Don’t forget to use [SerializeField] to be able to set the value through the inspector.

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.

If you want to know more about me, feel free to connect with me on LinkedIn or visit my website :D

--

--

--

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Recommended from Medium

Stop using Docker like a BFU

API economy and platforms in Finland — recipe for initial success

Introducing gRPC to our Hotels.com Platform — Part 2

Getting Better at Git: The Advanced Git Workflow

Improve your Python — Five features to include in your code

Summer 2018: Capital One

What Are Responsive Images And Why You Should Use Them

Cloud System Analyst

Cloud System Analyst

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Fernando Alcantara Santana

Fernando Alcantara Santana

A passionate computer technology engineer and Unity developer that is always looking to grow in every aspect of life :).

More from Medium

Setting up a Unity Project for the High Definition Render Pipeline (HDRP)

How to Build an Interactive Security Camera System in Unity

Introducing Stream’s New SDK for the Unity Gaming Engine

Enabling Unity Ads