Climbing Ladder- Game Dev Series 95
Objective: create a climbable ladder in game.
In this article, we will create a ladder that allows Player to climb up.
To finish this function, you need to prepare some elements before we make it works:
- A 3D ladder model- you can create one with cylinders or find some good model online.
- 2 model animation: 1 for ladder climbing, and 1 for climbing to top.
First let’s create a detection area to ladder. Select ladder model and create a box collider that cover up almost the bottom area of model.
This collider would be the detector. And also create a script and attach to it.
The first method we need is OnTriggerEnter().
That would be great for now. We will add more function to it later.
Player behavior on ladder
We need to define what should Player do when reached or touched ladder. Create a bool as definition of reached a ladder, and create a public method to enable the bool when it happened.
Once Player reached ladder, it should climb up when we pressed “W” or “Up” key, which is part of vertical input. Add an else-if statement inside Update() to make another exception besides walking and gravity affecting.
This is the basic of Player behavior, now we can import the climbing animation into Animator.
Create a transition from Running to Climbing Ladder.
Then create a bool parameter as the condition to start climbing. Then enable this bool in ReachedLadder(), the method we just made.
Call this method from OnTriggerEnter of Ladder script. Test it and you should have the same result as mine.
Sync the climbing animation with input
As you can see, Player started climbing animation right after we collided with ladder. However, this animation should be playing after we pressed “W” to climb up, not loop playing like this.
To make this animation sync with controlling input, we need to create another float parameter.
With this parameter, select Climbing Ladder animation and check the Parameter on Speed with VertiSpeed as Multipiler.
Then inside Player script, under the lines we defined vertical movement, add one more line to control this float parameter.
Now Player would do the climbing animation only when we pressed vertical input buttons.
Climbing to the top
The next is to make Player finished the ladder climbing animation. The setting would be pretty much the same as ledge climbing up animation.
Import the climbing to top animation and make a transition from climbing to it. Then create a trigger parameter “ClimbUpLadder” and assign it to the transition.
And don’t forget to create another transition from this to Idle.
Player should do the animation when we leave the collider of ladder, which is a good time to use OnTriggerExit() in Ladder script.
Now back to Player script and create a public method to handle the climb-up animation with the trigger.
We also need to disable CharacterController when we start the climb-up animation.
Once it’s done, call this method from OnTriggerExit().
Then we will need to create a empty Vector3 as standing position when Player climbed up. This part would be the same as what we did to ledge climbing.
Create another public method inside Player script to allow the climb-up animation to call it after the end.
Reposition Player to the StandPos by using the reference of Ladder(quick review here!) and enable CharacterController too.
The last part is creating an animation behavior to call this method when climb-up is done!
All done! Now you would be able to climb up the ladder!