Day 96 — Implementing a Ladder in Unity

Hey and welcome!

Now for something entirely new, it’s time to look into adding a ladder into the project so that the player can reach a ledge that’s just out of their reach when trying to jump to it.

This is going to be an interesting one to implement since we’ll need to add logic to our movement code to let unity know that our character is on a ladder so they can only move up or down, plus we’ll need to add in a couple of new animations that will need their own tweaking done to them.

Let’s start things off at by editing out movement code the Player script:

private float _prevSpeed;
private bool _onLadder;
void Start()
{
_anim = GetComponentInChildren<Animator>();
if (_anim != null)
{
_prevSpeed = _anim.speed;
}
}
void Update()
{
if (_onLadder == true)
{
float verticalInput = Input.GetAxis("Vertical");
_direction = new Vector3(0, verticalInput, 0);
velocity = _direction * speed;
if (verticalInput == 0)
{
_anim.speed = 0;
}
else
{
_anim.speed = _prevSpeed;
}
}
if (_controller.isGrounded == true && _onLadder == false)
{
}
}
public void ClimbLadder()
{
_onLadder = true;
}
public void OffLadder()
{
_onLadder = false;
}

To let our game know if the player is currently on the ladder we’re creating a new bool variable called _onLadder. Our regular movement code for moving left and right as well of jumping is all contained within the isGrounded if statement so in order to stop the code from executing that code when the player is on the ladder we now tell it to only use the regular movement if the player is on the ground and if _onLadder is false.

Inside the _onLadder if statement it’s nearly identical to the horizontal movement code but now we’re replacing it with only up and down vertical movement. One cool thing that we’re doing here that’s a bit new is altering the speed of the animator controller by setting it to 0 when we’re not moving and then setting it back to it’s initial speed that’s defined in the Start() method when we are moving.

In short this stops the player’s animation in the current post that they’re in so that we can have a more realistic looking ladder climb.

Speaking of the ladder go ahead and add that into the scene, making sure that it has both a box collider and rigidbody as usual:

With the box collider make sure that it doesn’t go all the way to the top of the ladder, this will stop the player from accidentally colliding with it when they’re near the edge.

When you’re happy with your ladder go ahead and create a Ladder script to add to it and then fill it with this code:

private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
Player player = other.GetComponent<Player>();
if (player != null)
{
player.ClimbLadder();
}
}
}
private void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
Player player = other.GetComponent<Player>();
if (player != null)
{
player.OffLadder();
}
}
}

These are some pretty simple trigger methods here since that’s all they need to be, all they’re doing is calling a specific method we created in the player script earlier to change the state of the _onLadder bool.

With that done you should have something like the above working for you. You’ll notice though that there are two problems here, there’s no ladder climbing animation and we also can’t get off the ladder! That’s something I’ll show you how to do in tomorrow’s article.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Connor Fullarton

Hey and welcome! My name is Connor and my goal here is to put out a daily post for a full year about my game development journey.