2.5D Platformer — Ladders #11
What a thrill…
Another addition to my Platformer project is Ladder Climbing! It’s not as thrilling or fancy as other games that properly aligns the hands and feet to the handles of the ladder, but it’s a “decent” start.
“Decent”, you say.
It’s okay… well no, it’s cheap and hardcoded, but coding a first iteration first hand lets me know how I would proceed to make something more polished. And sure, this implementation would work best for something like vines or other believable surface that doesn’t require matching the position of the hand with an object. But hey, I hope at least that you can too make something out of this article and do something greater yourself.
How does my implementation work?
To begin with, I added a new state to my character controller, which is “On Ladder” and two booleans to check if the controller is climbing a ladder in the current moment (isLadderClimbing), and if it is finishing the ladder climb (isLadderFinishingClimb).
I also made a new script for the ladder, which I’ll use to give information about that particular ladder to the controller. However, I’ll need two variables more to store the nearby ladder and the one the controller is currently climbing.
For the Ladder script to work, the Ladder GameObject requires three additional components:
- One or more collider triggers to assign the nearby ladder through OnTriggerEnter, and deassign with OnTriggerExit.
- One Empty child GameObject with a collider attached which serves as the travel bounds of that ladder. This is where the hardcoded stuff makes their way into the feature, and for every different ladder, you may have to adjust it for it to work as intented. Pretty bad, but it’ll do for now.
- Another empty GameObject to adjust a upwards position for the controller. If the controller leaves the Y max bounds of the travel bounds, it will play a climb animation and that requires teleporting the player to a position that matches the ending position of the animation.
Once the player controller enters one of the triggers of the ladder, the ladder script will pass a reference to itself and that way, the controller will have all the information it needs to start climbing the ladder.
Now that the controller has a way to know if it has a ladder nearby, we need to add an input to start climbing the ladder, changing the position of the controller to move it closer to the ladder, setting the new State to OnLadder, and so much more. Because there’s too much to actually explain, I’ve commented the entire Ladder Check for you to read.
Once the check is done and the controller has set its state to OnLadder, it’s just matter of moving the controller vertically, and checking if the player wants to jump from the ladder, if it touched ground again to leave the ladder, or has reached the top of the ladder.
LadderEndClimb() just removes the reference to the ladder and resets the isLadderClimbing to false, allowing the Controller to change it’s state to Grounded or OnAir.
And there’s a special method called LadderEndClimbAnimation(), which is called by another script when a Ladder animation has ended. This is used to move the controller to the top of the ladder.
It look, as I’ve said, decent. But it’s so disappointing from a code standpoint. Requiring to adjust manually each ladder is just going to lead to more hardwork in the long run.
However, now I have new ideas on how to improve it or scrap it for the “flexible” approach, which is defining the steps in the ladder and let the animation do the work for you (Dark Souls style).
Prototyping will give you enough insight which will allow you to grow your skills further. That’s how you climb the Game Dev ladder. :)