Hanging on a ledge!

Objective: Adding in a ledge grab system.

Rose Owen
6 min readJul 30, 2021

Now that we have the animation system up we can add in the animations for grabbing the side of a ledge and then pulling ourselves up. Animations are from Mixamo with slight tweaks to some of them to make the animations flow better.

If it really was as simple as adding in some animations to grab a ledge, this article would be a lot shorter…

Time to break down what we want to happen:

  1. Be able to jump from a ledge
  2. When close to the ledge, be able to grab onto it
  3. After grabbing the ledge, being able to press E to climb up
  1. This one we have already added, the only thing that wasn’t touched on was adding the animation. To add the animation, add a bool in the Animator for jumping. In the player’s script when we jump, use the animator set Bool to start the jumping animation. At the start of the character controller is grounded, set bool for jumping back to false.

With that, we have part 1 finished, onto part 2.

2. This one we kind of fake to a point. I’ll turn on the mesh renders so you can see what’s happening.

Notice how when the bar in front of the player comes into contact with the grey box on the ledge and right after that we grab the ledge? It’s part of the magic/smoke and mirrors to make all this work.

  1. Bar on player contacts the ledge contact
  2. ledge contact calls a method on the player
  3. player’s method turns the character controller off, set the velocity to 0, sets a trigger for grabbing the ledge, moves the player to the start position (this makes it seem like the player is holding onto the ledge, even when we didn’t land anywhere near the ledge) and changes a bool to say we are now on the ledge.

As you can see a lot happens on the player's script.

First the ledge grab script, we can put this on the ledge contact to make it more modular. All we need to add is 2 variables to store the start position of the player and for later, an end position. After that, we can add an on trigger enter that looks for the ledge bar on the player, using the bar we can get the player. Lastly, we can call the method for grabbing the ledge on the player putting the starting and end position in.

Now for the player. Here we need 2 extra variables, a bool for when we are on the ledge, and one to store the end position (more on this later).

Next, we can make the ledge grab method. It will be taking in 2 transforms, starting position and end position. Now we can set the y’s velocity to 0, turning off the character controller, setting the trigger for the animation to grab the ledge, setting the float for speed animation to 0 (this will stop coming out of the ledge grab and going to the running animation). For the variable we made for the end position, we can assign the incoming end position, change the transform of the player to the start position, and set the bool for the ledge to true.

Lastly for this part, setting up the animation. Here you can decide to either jump to a hanging idle or have an animation catch the ledge then go into a hanging idle.

So now we have this and the end of part 2.

3. All that is happening in this one is if we are on the ledge and we press E, pull the character up, and move the position to the end position (this is where this comes into play). Since we are looking for input, this part goes in the player's update method. All it is looking for is, if the player presses E and is on the ledge, set the trigger for climb and set the bool for jump to false (Setting the bool for jump can be done in the last part, either work).

For the animations, add in the climbing animation and have no exit time and a condition of the climbing trigger and then have exit time and link it back up to the idle.

So now we have this.

This is why we need that end position for the player, to get this we can press the pause button while climbing up and then use the next frame to get the right position for the player to move to. After getting that position we can put it on our ledge contact.

I ended up using empty game objects to hold the location to be able to make them more modular.

Next is to make a method to move the player to that location. All it needs to do is change the player’s position to the end pos and re-enable the character controller.

Now to call this method, one idea would be to make an event that when it finishes it calls this. However, how we are going to do it is to add a behaviour script to the animation which will call this when the animation finishes.

Click the Add Behaviour> New Script, give it a name and then open it up. Here you should see a lot of methods that are premade for us to use. The one we are after is OnStateExit, to use this one just delete the // before each line.

Here we can use the animator reference to get the Player from since the player is in the parent of this object. Next, we can check if the player is null and if it isn’t we can call the players after climb method we made.

Now we should get something like this which isn’t perfect but with a little tweaking of the exit time overlap, we can make it work.

This is with no overlap.

And with that, we have added a ledge grab system. To make movements smoother you can add in more animations like I did but you will soon end up with a lot of animations like I did.

Things get pretty complicated fast. Hope this has helped and I’ll see you in the next one!

--

--

Rose Owen
Rose Owen

Written by Rose Owen

Game Developer in the making~

No responses yet