Day 20: Building the foundation of the ladder climing mechanic
Objective: To build the first part of our ladder climbing system where we detect a ladder and we position ourselves on the ladder
To create this effect we need a few things. First we need to find a ladder model. This Ladder object will have have a total of 2 collider objects with each being placed at the opposite ends of the ladder. These cubes will serve as checkers for when a player is within range of climbing the ladder.
Both colliders will have a Ladder attached so that we can communicate with the player script.
This ladder script will also need public methods to return the Vector3 positions we want to be snapped to. We want to keep these variables private to the Ledge script only so these Public Vector3’s will simply feed these values into the player script.
Now that we have the base of our ladder script written, we can now communicate with the player to enable the ladder climbing ability after pressing the “E” key when the player is in range. Now we have to go into our player script.
at the top we will add some variables related to the ladder. When we are within one of the Ladder Checker objects, we will assign that ladder to the _currentLadder which will give us access to the Vector3 positions we need.
The player scripts will behave similarly to how we handle the grabbing of a ledge, only we will be able to move up and down. When we call the ClimbLadder(Vector3 _targetPos) method we need to turn off the Character Controller component so that we can snap our player to the correct position. We also need to rotate the player so that we are facing the ladder. To do this we simply tell the player script to create a local vector3 to this method and will use LocalEulerAngles to rotate the player -90 degrees so that we are facing the ladder. To call this method we will hit the “E” key when _nearLadder is set to true.
Now in order for us to move up and down we will modify our CalculateMovement() method to check for whether _nearLadder is set to true or false. If it is false then we will apply gravity as we normally do and we will calculate movement while grounded. Now because we turned off the Character Controller component we cant use the Move() method that is normally provided to us. To get around this we will use Tansform.Translate()method in its place. This creates an issue where the player can travel past the colliders of the floor but we will fix that in the next segment when we create a way for the player to dismount the ladder.
With our code now set in place to complete the ladder climbing ability, we need to update our animator controller on the player to play the ladder climbing animation. We will control this animation with the Climb_Ladder bool within the ClimbLadder(Vector3 _targetPos) method
It’s important to make sure our transition into the ladder climbing animation has no exit time and a transition time of zero to avoid any lag when snapping to the ladder. As mentioned earlier we will play this animation within the ClimbLadder(Vector3 _targetPos) method.
The full effect of our progress on the ladder mechanic can be seen in the .gif above. We have an issue where the player travels past the floor because we turned off the Character Controller component. We also have to complete the effect by giving our player the ability to exit the ladder when we reach the top or to simply dismount the ladder and return to normal gameplay if the the player chooses to. We will save those issues for the next segment.