Day 21: Finishing our work on the Ladder Climbing mechanic

Objective: To finish the code for our ladder climibing mechanic and to have the player play the correct animations for mounting and dismounting the ladder.
Now that we have the foundation for the ladder climbing mechanic, we can follow through with the hard part of setting up a way for the player to dismount the ladder and play the correct animation as well as snap to the correct position so that we can return our normal movement. Now playing the correct stepping off animation and snapping to different positions based on where the player is in relation to the ladder will take a bit of set up. First we need to modify the ladder that we built to have the components seen in the image below.

I set up numerous trigger objects to help us determine what position the player is in. The box collider marked green is the main ladder component which contains the Ladder.cs script. The box colliders marked purple will serve as triggers to tell the player that we have reached the end of a ladder and play the correct stepping off animation. these trigger zones will serve as checks for when we reach a stepping off point. The box collider marked yellow at the top of the ladder is a special trigger zone that will tell the Ladder that our player is trying to enter the ladder from above. This trigger zone waill contain its own script called LadderTop.cs which will communicate with both the player and the main ladder object. The final box collider is labeled as Ladder_Cover, which we will activate and deactivate when the player interacts with the ladder. The ladder cover object will not be a trigger so that we can collide with it when it is active in the scene.
After we have our ladder object set up and ready to use, we will need to update the players animator controller so that we can call the correct animations. We will add two trigger parameters corresponding to the player’s position relative to the ladder if we come into contact with the Exit colliders which are marked purple. The animation labeled “Turtle_Climb_Ladder” is actually a blend tree state. This blend tree will have the player transition between being idle on a ladder to climbing up or down the ladder with movement.

The blend tree will give priority to the correct animation based on the speed parameter which we are updating differently since the player is moving vertically now.

I wont go into detail with how to properly set up each transition for the animations related to climbing a ladder because that could be a whole article on it’s own. However, scanning the code should guide a developer on how to properly use the parameters to animate the player properly.
Once our set up for the player is complete we can move on to the code. The Ladder script will require a few more variables and methods on top of what we already built.

We will need Vector 3 references to our desired mounting position from the top of the ladder, one fore when we are at the base of the ladder, and _airPosition is a reference to where we want to snap our player to if we mount a ladder in the middle of a jump. _standPos is a refence to where we dismount the ladder if we exit from the top. _stepOffPos is a reference to where we snap to when we exit from the bottom.

We will create these functions which will return the proper Vector3 position to the player when we need to decide where to snap the player to. These functions will simply return the values we set in the editor. To find the position we need to snap to in when we are trying to mount while we are mid air will require a bit more code.

Our air position will take in 2 float values from the player representing the x and y values of a vector 3. The z value will be equal to that of the Vector3 we use to mount from the base of the ladder. we do this so that we snap to the center of the ladder if were mid air and we will keep our X and Y values to equal that of the player objects transform.position. The next function will return the Vector3 position we need to snap to when we mount the ladder.

If we are not entering the ladder from the top we use the public Vector3 MountLadderPos() to snap the player to a mounted position if we are at the base of the ladder or if we are mid air. The last two methods are simply used to turn on and off the colliders we need depending on whether or not the player is on a ladder.

The player will call these methods when we mount or dismount the ladder. The funcionality can be seen in the gif below. We will activate the cover object only when we are not currently on the ladder. The same is true for the exit objects as we wont need them to be active if we aren’t currently on the ladder. Having them active from the start will cause logical issues while we interact with the ladder. These exits will also need to be tagged as such so that the player can determine which animation to play. It’s important to position these colliders properly to acheive the desired effect.

The collider at the very top will contain the LadderTop.cs script. All this script does is flag the player for being at the top of the ladder so that when we mount the ladder we enter from the top mounting position.

Now that our Ladder code is complete we need to turn our attention to the player script so that we can call these methods appropriately. We will need to add the appropriate variables related to the ladder as seen in the image below

The update loop in the player script can now make use of the Vector3 positions we defined in the ladder script. We determine the position of the player using the character controller’s grounded state along with the _LadderTopFlag that we control with the Ladder_Top trigger inside of the Ladder object

Now inside of the ClimbLadder() method in our player script that we created in the last segment we will add the code seen in the image below.

If we are not on a ladder and if we are not currently dismounting a ladder then we will move the player vertically r]to simulate climbing up and down. During this state if we press the “E” key while we are on a ladder we will dismount the ladder from wherever we are and fall off the ladder. If these conditions are not meant then we apply gravity as we normally do and we move the player as we normally do. We also turn off our _airCollider and _airBody components because we rely on our character controller component to handle collisions when running and jumping.

The next method we need is the LadderCheck() method which we call in the Ladder script to tell us if we are in range of a ladder. We also flag for top entry so we can enter the ladder differently if we are coming from above.

This next method is what we call when we are ready to mount the ladder. This method will be called from the update loop in the player script and takes in a Vector3 to reposition the player with. We also set the bool needed to enter the blend tree in our animator which controls the ladder climbing animation.

The method ExitLadder() is what we will use to trigger the stepping off animations. We call this method inside of our OnTriggerEnter() and OnTriggerExit() methods. Will only interact with these objects if we are mounted on a ladder so we activate a separate rigid body and capsule collider for that purpose. when we are running and jumpin as we normally do we rely on the character controller component.

This next method will only be called if we hit the “E” key while we are mounted on a ladder. this will cause us to simply let go of the ladder and fall down. The next method is a public method that will be called by a StateMachineBehaviour we will assign to the stepping off animations

We will add state machine behaviours to our stepping off animation exactly the same way we handle climning up from a ledge. for that reason I wont go over the code in these scripts. They both snap the player to the correct position when the animation reaches the end state call back.

The last two methods of our script arent anything we havent seen. The ClearFlag() method is called when we exit the trigger of a ladders Ladder_Top trigger. The OnTriggerEnter() method simply calls the exit ladder method and it tells us if were exiting from the top or bottom of the ladder.

So now with all of our code and set up complete we should acheive the effect seen in the gif at the top of this article. The gif below is used to illustrate why we need the Ladder_Cover object. It provides a platform for the player to stand on if they choose not to interact with the ladder and move on.
