Day 97 — How to Climb up from a Ladder
Hey and welcome.
Now that our player can climb up and down the ladder it’s time to figure out a way to climb up from it as well as add in some animations! Let’s go ahead and kick things off here by grabbing a couple of animations from mixamo. The first one we’ll need is a regular ladder climbing animation that loops in place:
Then there’s a follow up animation called Climbing To Top that we can use after this:
Import those into Unity the usual way, double checking that it’s a humanoid rig and that the first animation is set to loop etc and then add them both to your animator controller for the player.
Position your new animations as seen above and create two new parameters, a bool LadderClimb and a trigger LadderClimbUp. Add the first parameter as a true condition for your transition between Running and Ladder Climb and then add the second to your Ladder Climb to Climbing To Top animation.
To handle the climbing up from the ladder part we’re going to be doing things similar to our ledge grab by creating a cube that our player’s ledge checker object can collide with in order to start the animation.
Name it along the lines of Ladder_Top_Checker and adjust your cube until you’re happy with its placement and make sure that it’s a trigger collider with a rigidbody and that you’ve added it as a child to your ladder object. Now create a new script called LadderTop that you can apply to this and then add the following code in:
if (other.tag == "Ledge_Grabber")
{
Player playerController = other.transform.parent.GetComponent<Player>();if (playerController != null)
{
playerController.transform.parent = this.transform;
playerController.GrabLadderTop(_handPos, this);
}
else
{
Debug.LogError("Can't find Player script");
}
}
Pretty much identical to what we’re using already except for the method from the player script that we call. Speaking of the Player script go ahead and make these edits to the ladder climbing code:
private bool _ladderClimb;if (_onLadder == true)
{
float verticalInput = Input.GetAxis("Vertical");
_direction = new Vector3(0, verticalInput, 0);
velocity = _direction * speed;
_anim.SetBool("LadderClimb", true); if (verticalInput == 0 && _ladderClimb == false)
{
_anim.speed = 0;
}
else
{
_anim.speed = _prevSpeed;
}
}
Here we’re creating a new _ladderClimb bool variable which will come in handy later as it prevents our Climbing To Top animation from pausing midway through. We’re also now activating the regular ladder climb animation so that things look a bit more realistic!
Next is the ability to climb off the ladder so go ahead and add in these two methods to the Player script:
private LadderTop _activeLadderTop;public void GrabLadderTop(Vector3 handPos, LadderTop currentLadderTop)
{
_controller.enabled = false;
_ladderClimb = true;
_onLadder = false
_anim.SetTrigger("LadderClimbUp");
_anim.SetFloat("Speed", 0.0f);
_activeLadderTop = currentLadderTop;
transform.localPosition = handPos;
}public void LadderClimbComplete()
{
_ladderClimb = false;
transform.localPosition = _activeLedge.GetStandPos();
_anim.SetBool("LadderClimb", false);
_controller.enabled = true;
this.transform.parent = null;
}
This is a bit of list of code here but it’s all making sure that climb to top animation is the focus and that the position of the player when they’re at the top of the ladder starting the animation and then on the platform after completing it is using Local Positions when they’re parented to the Ladder_Top_Checker.
Last bit of code next, go into your animator and create a new behaviour for the Climbing To Top state and add in this code so that we can make use of the LadderClimbComplete() code when the animation finishes.
override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
var player = animator.gameObject.transform.parent.GetComponent<Player>(); if (player != null)
{
player.LadderClimbComplete();
}
}
Now the last thing to do is to get the positions for the _handPos and _standPos that you’ll add in through the inspector. Get to the animation you need and pause the game and then position your character in the positions you want them to be in. (Remember you can join animations to the Any State if you need to play them on command!)
When you have the position you want make sure you parent the player to the Ladder_Top_Checker object and then take a note of the x, y and z positions here and add them to your _handPos and _standPos positions for your ledge script in the inspector.
With that all together and done you should have something like the above working for you!
Be sure to prefab the ladder object as well since we’ve already implemented the necessary code to make this modular and usable regardless of where we place it in the scene. Also apply the prefab overrides to your Player object as well since we made changes there too.