Framework 1: Climbing A Ladder
Now it is time to make your character have the ability to scale a ladder. Although this logic behind this concept is not new to gaming, the procedure to make this happen is quite hard to script. It will require a lot of script communication and switching of booleans.
Before we start scripting, we are going to have a quick breakdown on what is going to happen. First the player is going to make contact with the ladder game object, the ladder, which houses two trigger events, one for on Enter, and one for on Exit. These two trigger events are going to communicate with the player script to conduct two different scenarios.
On Trigger Enter, the Player is going to trigger a boolean switch that will allow several IF statements to occur. The first statement is going to set the gravity to zero, make the climbing animation happen, and also snap the character position to a preset location. It is also going to set the directional input to Vertical instead of Horizontal. Finally we need some “Mathf.Abs” conversion for going up and down the ladder.
On Trigger Exit, the player is going to turn off the Character controller, and play an animation of “Climbing over the ledge”.
Finally there is going to be one more script, and that is a Animation Behavior script that will trigger at the precise moment of an Animation transition, which allows the player to turn back on Character Controller, and snap into the finishing position.
That is all for the logic, now it is time to start scripting.
Preparing the Climb:
First we are going to download two animations from Mixamo website. One for ladder climbing, and one for climbing on top.
Similar to how we handle all the other animation clips, we are going to follow the same procedure as we have up to now. These are also how I set the animation clip before adding them to the Animator.
And the Animator Layout I had this setting where I had Running > Climbing > Climb top. I also had the same transitions but starting from Idle > Climbing > Climb top.
Finally, before we start scripting, we need to add some parameters to the animations. Add a Boolean named “Ladder” and a Float named “Movement” with a value set to 1.
On your Ladder game object, we are going to prepare several things, first create two box colliders on the Ladder model itself. The first box collider is a physical settings, so your player doesn’t run through the object. The second box collider is going to be the Trigger mechanic for the climbing animation. This trigger mechanic is going to be only half the height of the ladder model, but it extends outward just a bit so it can collide with the player easier.
Then we are going to create two empty game objects that are going to be the position place holders that our player will snap to. One on the bottom, and one on top.
To finish off, we are going to organize the Hierarchy a little bit. Create a new empty object called “Ladder_systems” which will house all 3 objects. Be sure to not have the target objects a child of the ladder model.
Scripting the Ladder Behavior:
Create a new C# script and named it “Ladder” then attach the script to the “Ladder Model”. This script’s main function is detect whenever the player makes contact with the ladder. If the player does make contact, we are going to have two Event Triggers.
First create an serialized field looking for two game objects, these two game objects are going to be the Target position place holders.
On the Trigger Enter event, we are going to pass the game object data to the player script and also reference an player public method(which we will create later)
On the Trigger Exit event, we are going to reference another public player method. (Again, we will create these new public methods later)
The Player Script:
In the player script, we are going to first create several local variables.
Then we are going to create some public methods, these are the same methods which the Ladder script and Animation Behavior Script will need access later.
First is the Climb Ladder method which the Ladder script requires. After passing in the two game object data, we are going to convert them so the player can have access to them. Then we are going to set the animation Speed to zero along with trigger the On Ladder boolean true.
Next is the Exit Ladder public method. This is what the On trigger Exit event from the Ladder script requires. Its main function is to turn off the On Ladder boolean and also turn off the climbing Animation. We are also going to set an IF statement here which checks for the boolean “Climbing Ladder”, it that boolean is set to true, we can disable the Character controller.
Finally for the last public method, this is for the Animation Behavior script that promises the precise trigger of this script as the animation transition happens.
On this function, we want to snap the player to the top ladder position while turning on gravity again along with the Character controller. We will also be setting the Boolean “Climbing Ladder” to false.
You might be confused as to why some of these settings are mentioned even thought they were set prior, this is because we haven’t touched the most important part of the ladder climb script. And that is movement, we need to turn off the Horizontal movement and replace it with Vertical movement. This can only take place within the Update method.
On the Update method, we create the If statement which check whenever the Boolean “on Ladder” is set to true. This only occurs when the player makes contact with the ladder model. When that happens we are going to turn off gravity, set the climb animation to play, snap the position of the player to the bottom target position.
Then we are going to replace the Horizontal movement with the Vertical movement. Along with the calculation of movement value. So the Animation plays if we are going up and also when we are going down the ladder and ONLY when we move does the animation play.
Then we finish up with an else statement of where if gravity = zero, then the animation speed will also be zero.
The Animation Behavior Script:
To finish off everything, we need to create a new Animation Behavior by clicking the “climb to top” animation state in the Animator, then in the inspector click the “Add Behavior”
Inside the script, we are going to unlock the On State Exit function and basically gain access to the player script. So we can make a reference to the final public method “Off Ladder”.
Once all three scripts has made their proper linkage, the ladder climb function should be working.
You might need to adjust some settings with the Animator itself such as unchecking “Has Exit Time” during the transitions, and also adjusting the duration of the transitions.