2.5D Platformer — Ledge Grab & Climb #9
No height is too high!
I recently made a *very* basic implementation of a Ledge Grab and Climb which allows my character to reach high places easily. This implementation allowed me to understand the process behind this feature and allows me to create a more polished implementation of it. So I thought this would be worthy of being in an article.
Also, there’s a bit of a time skip between this article and the last one, but you didn’t miss much as all I did was just setting an animator controller for the character. Sorry about that.
The Logic Behind It
To give information to our character controllers about their surroundings, the easiest way to do it is by casting some rays or SphereCasts around them. First, the controller checks for a collider in front of it while it’s in the air, and then, it checks if it’s possible to climb up that collider and if there’s enough space for the controller.
The (Basic) Implementation
I’ve decided to go for another implementation, which requires two new scripts, and two new GameObjects: a ledge checker trigger, and a ledge trigger. I won’t need to code any Raycasts, at the cost of making a dirty, fast, and cheap ledge grab and climb.
You’ll want to create and adjust a new Cube to fit your character’s hanging animation. Set the collider to trigger and add a new tag (name it something like “LedgeGrabChecker”).
The ledge will also be a cube with a trigger collider, but this one will have a Ledge script. When the LedgeGrabChecker collides with it, it will call a method within the Character Controller I named LedgeGrab.
Let’s see how this works…
Not too bad. Doesn’t look that great, but keep in mind that the animations aren’t that compatible with eachother and this implementation doesn’t allow too much room for improvements. However, for a small project or game, it’s “fine”.
Moving on. The next step is to make the character climb up the ledge with an animation and block any input until the controller has finished climbing up.
So, how can we tell the controller the climbing up animation has finished so we can start moving again? With a new Animator Behaviour script! You can create a new one by clicking on Add Behaviour in one of your animation blocks.
This script will auto-generate code blocks with commentated methods. You’ll want to remove everything except the OnStateExit method, which will signal the controller as soon as the Climbing animation finishes.
And now, in the LedgeClimb method, offset the position of the character controller to move it approximately where the character’s model is, and reset the OnLedge and Climbing states.
And, with this, the ledge grab and climb is finished! You’ll have to adjust some values to fit this implementation to your assets and your liking, but you should be set to use this in your game.