Unity Development — Ladder
As you most likely have discovered by now, everything that seems very simple in real life becomes a more complicated issue when going to program it into your game. There is even a joke among game programmers on the internet about doors being very difficult to bring to life in a game. Today we’re going to attempt the ladder.
When it comes to level design and other game development aspect, the ladder is another good tool to have under your repertoire. As usual, you are going to want an animation or sprite of the character climbing a ladder, as well as a ladder model itself. Place this ladder in between two platforms. In the Hierarchy, add two children, representing the bottom and top of the ladder. Each should have their own trigger collider attached.
Each one of these children will have a ‘Ladder’ script on it to perform the OnTriggerEnter for when the Player gets near one of the ends and enters the triggers. To allow for the ladder to become a prefab to have as many as we want, our variables in script with be serialized, so they can be changed. This include the two Vector3s and two floats. These will represent where the player will grab on to the ladder, where they will stand when getting off and the min and max height on the ladder.
When the player enters the trigger, these will pass into variables set up on the player in a public function that will be called from the ‘Ladder.’ They will be used as parameters when calling that public function.
Once the player’s _nearLadder bool is set to true, they should then be able to hit a key to climb the ladder. Other games have it that you can simply hold up and be on the ladder. What you choose is up to you, but this example will show an input to get on the ladder. Once the player hits ‘E’, it will call another function on the player that sets up the OnLadder animation as well as set up their bottom of the ladder position. If they grab the ladder from the top, it will set up them up on the top section’s grab position.
The ‘Ladder’ script contains a public function to get the grab position of the active ladder. This is called when getting on the ladder. It is a simple return call.
When at an edge and hitting the ‘E’ button again, it will perform almost the same tasks in reverse. The player will go back to idle and it will call the ‘Ladder’ to get the new standing position.
Since the player’s movement is so different when they are on a ladder, that needs to be setup wherever you are performing the player’s movement. In our case, we use the _OnLadder variable to set this up. Instead of being grounded and using horizontal movement, the player uses vertical inputs to move up and down. Because they are on a ladder, this movement has to be clamped, so as to prevent them from going too high or too low.
It would also be a good idea to turn off all gravity if they are on the ladder. That way the player isn’t fighting against it going up.
And that should just about do it. You may have to change some rotations on the player, depending on how you set up your ladder, but the bones of the task should all be about the same. If you wanted to design your version of Shoots & Ladders, you now have another key.