How to do a "standing on Ladder animation"

You surely thought man the idle animation on the Ladder looks dorky. Well i thought the same so heres a way to freeze an animation i found out. You can do that similiary to the SetBool function of the animator.
So heres the first thing you gotta do.


Go to your Animator and add an Float parameter just like you did with your bools. Let’s call it climbingSpeed or something like that.

Than click on your climbing animation inside of the animator and check the parameter checkmark for speed like shown here. In the dropdown menu set it to climbingSpeed.

Now lets get into our Player Script again.

add

[SerializeField] float climbAnimationSpeed = 1f;

and your ClimbLadder method should look something like this.

void ClimbLadder()
    {
        bool playerHasVerticalSpeed = Mathf.Abs(myRigidbody.velocity.y) > Mathf.Epsilon;
        int ladLayer = LayerMask.GetMask("Climbing");

        if(myCollider.IsTouchingLayers((ladLayer)))
        {
            Vector2 climbVelocity = new Vector2(myRigidbody.velocity.x, moveInput.y * climbSpeed);
            myRigidbody.velocity = climbVelocity;
            myRigidbody.gravityScale = 0f;
        

            if(playerHasVerticalSpeed)
            {
                Debug.Log("Climbing");
                myAnimator.SetBool("isClimbing", playerHasVerticalSpeed);
                myAnimator.SetFloat("climbingSpeed", climbAnimationSpeed);
            }

            if(!playerHasVerticalSpeed)
            {
                Debug.Log("Standing on Ladder");     
                myAnimator.SetFloat("climbingSpeed",0f);
            
            }
        }
        else
        {
            Debug.Log("NotClimbing");
            myAnimator.SetBool("isClimbing", false);
            myRigidbody.gravityScale = standardGravityScale;
        }

Check with the Debug look if the states are working.

Now when you’re standing on a ladder the animation freezes.
For more Info on the SetFloat method Unity - Scripting API: Animator.SetFloat

7 Likes

Great way to do the animations. Thank you for sharing this. This will be really helpful.

2 Likes

Great job! :+1:
Using your method, I also managed to reverse the climbing animation, so when you climb down, the character didn’t use the same animation as for climbing up.

I just added the Mathf expression Rick used earlier: (in ‘playerHasVerticalSpeed’ if statement)

myAnimator.SetFloat("climbingSpeed", climbAnimationSpeed * Mathf.Sign(myRigidbody.velocity.y));
1 Like

Love this. If you want to set it so the player doesn’t enter the climbing animation you need a bit more fiddling but I’m content for now.

This can also easily be done by just pausing the animator:

if (!canClimb())
{
        myRigidbody2d.gravityScale = gravityScale;
        myAnimator.SetBool("isClimbing", false);
        myAnimator.enabled = true; // make sure to re-enable it if exiting early
        return;
}

bool pressingClimbKey = Mathf.Abs(moveInput.y) > Mathf.Epsilon;

myAnimator.SetBool("isClimbing", pressingClimbKey);
myAnimator.enabled = pressingClimbKey; // only activate when they are pressing the climb key
5 Likes

Great work, love it. I did some ChatGPT work and figured out the following solution, which allows the character to climb the ladder with “isClimb” animation playing and pausing (when he ‘hangs’ there).

void CLimb()
    {
        if (!IsOnLadder())
        {
            myRigidBody.gravityScale = defaultGravityScale;
            myAnimator.SetBool("isClimbing", false);
            return;
        }
        
        myRigidBody.gravityScale = 0f;
        Vector2 climbVelocity = new Vector2 (moveInput.x * runSpeed, moveInput.y * climbSpeed);
        myRigidBody.velocity = climbVelocity;

        if (!IsOnGround())
        {
            bool playerHasVerticalSpeed = Mathf.Abs(myRigidBody.velocity.y) > Mathf.Epsilon;
            myAnimator.SetBool("isClimbing", playerHasVerticalSpeed || IsOnLadder());
            myAnimator.speed = playerHasVerticalSpeed ? 1.0f : 0.0f;
        }

        else
        {
            myAnimator.SetBool("isClimbing", false);
        }
    }

To prevent the animation speed from remaining 0 after the character goes off the ladder, we need another check in Update() method:

void Update()
{
    //other methods
    if(!IsOnLadder())
    {
        myAnimator.speed = 1.0f; // Reset animation speed to normal when not on the ladder
    }
}

Privacy & Terms