Nerd For Tech
Published in

Nerd For Tech

Creating A Ledge Grab in Unity

Now that we have our character animated, let’s take a look at how we can go about creating the animation and process for ledge grabbing within the game. To start, we will need add an animation for hanging off of a ledge. From there, we need to create an object that we can use as our detector for when we get close to the ledge that will allow us to hang onto it:

Now that we have our ledge checker set up on our character, we will need a way to save it to our hierarchy, as we used play mode to create this checker. To do so, we can simply make it a prefab in our assets folder, then just replace it in the hierarchy with that prefab when we stop the play mode:

Now that we have a checker built, we need to figure out how we can create a ledge checking system. In order to do so, we will first need to create a cube for us to use as our detector and line it up with roughly where our character will reach when we attempt to jump to the ledge:

Now that we have our objects set up for initiating our ledge grab animation, we now have to code in what we want to do once we hit the ledge. To start, we will want to work within our scripts to create a connection between the triggers. Within our player script, we will create a new void that will freeze our player in place and activate our animation when it is triggered:

As for when it gets triggered, we will create a new script that will activate this void upon being triggered:

Now whenever we have our detector trigger the grab point, our player should freeze in place and go into an idle hanging animation:

Now that we have this, the next step we will want to take is figure out how we can snap our player into a more appropriate position. To do this, we first need to move our character into a position that looks good to be grabbing onto and saving those coordinates:

From here, we will create a new variable within our trigger script and set these coordinates as the location that our player will snap to upon reaching the trigger:

Now that we have it set up, and we place the values within the trigger’s new variable to work with, we can test it within our game:

Now for the tiny details portion of this feature, we need to find a way to make the jump look as smooth as possible. As it stands now, the character snaps a bit too much into the position and it doesn’t have a natural feel to it, so in order to fix this, it involves a lot of trial and error in jumping over and over till we get a good looking ledge grab. After a lot of trial and error of figuring out a good transition between states, you can get something similar to this:

Now that we have our process of grabbing onto a ledge all sorted out, we can look towards how we can animate our player to pull themselves up to the platform.

--

--

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