Jumping is an essential part of many video games, platforming your way through different levels. How good does it feel when you don’t make the jump, but the character grabs the ledge and clings on to the ledge? It adds to the movement and maybe some of the excitement. Let’s create that moment together.
This article will not be covering the basics of moving a character, their jumping, and basic animation. My previous articles have covered this before, so we are going to start with already having a character that can do all of this. Add in a hanging and climbing animation to that player’s controller and we’ll start from there.
Make two platforms out of cubes some other objects if you have the art for it. On the platform you are testing your ledge grab, create a trigger collider that extends out of the actual ledge. This is the area in which the player will be able to hit and grab on to the ledge. You may also want to put this in a child of the platform.
Do the same thing for your player. Create a trigger above their hands, as if it was where their hands would be when hanging.
Now create a script for the ledge trigger that will call OnTriggerEnter for the player. You won’t be able to make all the changes for the ledge grab from here, so it is best to make a public function on the Player and call to it. If you put the player’s trigger in a child, you can use their tag and their hierarchy to reach the Player script. It should look something like this.
That _grabPosition variable that you see being used as a parameter for the player’s public is the position that the player should snap to in order to make the ledge grab look like they are holding the platform. The second parameter is the specific ledge itself. This is done so the player will know the specific ledge and and we can make this a prefab later.
The public function on the player should make sure the player can’t move when hanging and make sure the Animator makes all adjustments. This is best done by turning off the controller and setting the proper Animator parameters off. The player’s position should also be set to the grab position sent by the ledge.
Using a bool to know the player is hanging and setting up the active ledge which platform they are on, we can then use Input in this condition to climb up. Once again, the ledge script should have a variable that can be changed specifically to this object to represent the new position of the player. This can be called as a public function since it was made the active ledge.
As a Serialized field, this can be changed in the editor for each platform.
This public function will then be called after the input.
This will get the animation for the climb up going. There are multiple ways you can do the next step. You can either make a public function on Player and set it up as an event in the ‘ClimbUp’ animation, or give that animation itself a script and have it call that public function on Player. Today we are going to use the latter.
When you make a script on an animation in the Animator, it gives you built in functions that specifically for animations. The one we are going to use now is OnStateExit(). This will happen once the animation is being exited. In our case, it is going back to Idle and will then be called.
Here we are again finding our way to the Player script and then if it exists, calling the public ‘ClimbUpComplete’ function.
This function changes the position to be where the player should be right after climbing up from the ledge, sets the LedgeGrab animation off, and enables the Character Controller back on for movement. Overall this long process creates the action intensifying ledge grab.
You can play around with timing and the animation transitions, but everything you need is there. Have fun with it and make some leaps of faith.