Climb Ladder: My player gets stuck in climb ladder animation

S14 L329: Climb Ladder: My player gets stuck in climb ladder animation after climbing the ladder and leaving the ladder (I implemented a separate jump animation on my own)

Here’s a screenshot of what happens with code below:

Here’s the player script code:
Here’s my code (with Jump modified to allow a jump animation to show and stop properly. I’m guessing my problem is something to do with the conflict between Jump state and Climbing state?) Please help…


using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityStandardAssets.CrossPlatformInput;

public class Player : MonoBehaviour {

// config parameters

[SerializeField] float runSpeed = 8f;

[SerializeField] float jumpHeight = 5f;

[SerializeField] float climbSpeed = 5f;


// State variables

bool isAlive = true;


//cached component references

Rigidbody2D myRigidBody2D;

Animator myAnimator;


// state variables

Collider2D myCollider2D;

// Use this for initialization

void Start () {

    myRigidBody2D = GetComponent<Rigidbody2D>();

    myAnimator = GetComponent<Animator>();

    myCollider2D = GetComponent<Collider2D>();

}

// Update is called once per frame

void Update () {

    Run();

    LookLeftOrRight();

    Jump();

    ClimbLadder();

}


private void LookLeftOrRight()

{ // if the player is moving horizontally...

    bool playerHasHorizontalSpeed = Mathf.Abs(myRigidBody2D.velocity.x) > Mathf.Epsilon;

    // mathf.epsilon gives an almost zero value.


    if (playerHasHorizontalSpeed)

    {

        transform.localScale = new Vector2(Mathf.Sign(myRigidBody2D.velocity.x), 1f);

        //localScale is simply the scale parameter in inspector under transform.

        // reverse the current scaling for x axis

        // use mathf.abs to get a positive number.

        // use mathf.sign to give a negative sign or positive.

    }

}


private void Jump()

{

    if (!myCollider2D.IsTouchingLayers(LayerMask.GetMask("Ground")))

    { return; }


        if (CrossPlatformInputManager.GetButtonDown("Jump"))

        { // give ourselves a new y velocity:

            Vector2 jumpVelocityToAdd = new Vector2(0f, jumpHeight);

            myRigidBody2D.velocity += jumpVelocityToAdd;

        }


        // My code that gets jump animation working and stopping when it should:

        bool playerIsJumping = Mathf.Abs(myRigidBody2D.velocity.y) > Mathf.Epsilon;


        if (playerIsJumping)

        {

            myAnimator.SetBool("Jumping", true);

        }

        else if (!playerIsJumping)

        {

            myAnimator.SetBool("Jumping", false);

        }

   

}


private void Run()

{

    float controlHorizontal = CrossPlatformInputManager.GetAxis("Horizontal");

    // value is between -1 (left) and 1 (right)


    Vector2 playerVelocity = new Vector2(controlHorizontal * runSpeed, myRigidBody2D.velocity.y);

    // controlHoriz * runSpeed makes it from -8 to +8

    // seems it HAS to be set manually in unity. so i matched it to 8.

    myRigidBody2D.velocity = playerVelocity;


    bool playerHasHorizontalSpeed = Mathf.Abs(myRigidBody2D.velocity.x) > Mathf.Epsilon;


    myAnimator.SetBool("Walking", playerHasHorizontalSpeed);

    // instead of this if, you could also replace the bool variable name where it says true

    // which equates to the same thing. Done this one.

    // original code: if (playerHasHorizontalSpeed) { myAnimator.SetBool("Walking", true); }

    // the new line that includes playerHas...in the SetBool has the added benefit

    // of stopping the player from having the run animation when horizontal speed stops.

}


private void ClimbLadder()

{

    if (!myCollider2D.IsTouchingLayers(LayerMask.GetMask("Climbing")))

    { return; }


        float controlClimbing = CrossPlatformInputManager.GetAxis("Vertical");

        // get up/down input and assign to controlClimbing variables.

        Vector2 climbVelocity = new Vector2(myRigidBody2D.velocity.x, controlClimbing * climbSpeed);

        // set climb veolcity to what it already is on x-axis, and on the y-axis, get vertical input

        // and multiply by climbSpeed which we've set to 5 for now but is serialized (editable in unity).

        myRigidBody2D.velocity = climbVelocity;


        bool playerIsClimbing = Mathf.Abs(myRigidBody2D.velocity.y) > Mathf.Epsilon;

        myAnimator.SetBool("Climbing", playerIsClimbing);

   

}

}


Thank you in advance to anyone who can help.

My apologies: The tutor’s animation also is stuck on climbing as of this lecture. I shall see if he resolves it in the following one.

Please delete this question. Resolved in the next lecture.

Privacy & Terms