Ladder Animation in Unity
Objective: In Unity create a system to allow the player to climb a ladder.
Import Climbing Ladder and Climbing To Top animation from Adobe Mixamo as FBX for Unity files, checking in place when available. Drag these into the Man_01 FBX folder. Select each xbot@ file and set animation type to humanoid on rig tab and apply, for example Climbing To Top below.
Drag these animations into the Animator window and create transitions from Sad Idle to Climbing Ladder, from Climbing Ladder to Climb To Top and from Climb To Top back to Sad Idle, as shown below. Create two new parameter a bool Ladder used to switch into and out of Climbing Ladder animation and Movement which is used to animate going up and down the ladder.
The Climbing Ladder animation uses the movement float to modify aniamtion speed, moving up when +1 and down when -1. This efevtively reverses the animation.
The Climbing To Top animation is shown below and contains the Ladder Behavior script.
The Ladder Behavior script is shown below. This animation has root motion communicates with the Player script on exiting the animation to provide smooth animation.
Create an empty game object, Ladder_Model, and attach a C# script named Ladder to this object. Next import a ladder prefab from GameDevHQ Filebase and child it to Ladder_Model, positioning as desired. The Ladder_Model parent allows the model to be more easily positioned regardless of the prefabs pivot point.
The Ladder_Model needs two colliders, one to act as a trigger for climbing and another to prevent the player from running through the ladder. The is trigger collider should be a little wider than the other box collider. The is trigger is about half the height of the ladder because exiting the trigger is used to initiate the Climb To Top animation.
Create two more empty game objects, Ladder_Top and Ladder_Bottom, positioning them at the top of the ladder and the bottom of the ladder. These are snap to positions for the player when entering and exiting the ladder system. Now create another empty game object, Ladder_System, and child the Ladder_Model, Ladder_top and Ladder_Bottom to this object, see below. Do not put the Ladder_Top or Ladder_Bottom inside the Ladder_Model.
The Ladder script controls the behavior for the ladder, as shown below. The script has a variable for the the _top and _bottom of the ladder, which are assigned in the inspector, as shown above. This script detects when the player enters or exits the is trigger box collider, and communicates with the Player script.
The methods on the Player script controlling the ladder movement are shown below. ClimbLadder is called from the Ladder script’s OnTriggerEnter method, setting the _onLadder bool to true. The Ladder script also passes the positions for the top and bottom of the ladder, providing a modular design. The ExitLadder controls the Climb To Top animation from the Ladder script’s OnTriggerExit method, turning off the character controller, _controller, only if _climbingLadder is true. Finally upon exiting the Climb To Top animation the LadderBehavior script triggers the OffLadder method restoring _gravity and the _controller.
The Player script methods require two new bool variables to control animation, two new vector3s to control player position and a float, _ladderSpeed, used later.
The movement up and down the ladder is controlled in void Update(). If the Player is _onLadder and gravity is on then the player sets _climbingLadder to true and turns off _gravity, snapping the Player to the bottom of the ladder. If vertical input is detected the _anim.speed and Movement parameter are set to control the direction of the animation, otherwise animation is frozen by setting _anim.speed to zero.
This ladder system allow movement by the player on the ladder.