1

I am trying to make the player to climb a ladder. (2D game)

I used this code for that,

void OnTriggerEnter2D(Collider2D collider){
    if(collider.gameObject.tag=="Ladder"){
        _canClimb = true;
        _anim.SetBool("Climb",true);
    }
}

void OnTriggerExit2D(Collider2D collider){
    if(collider.gameObject.tag=="Ladder"){
        _canClimb = false;
        _anim.SetBool("Climb",false);
    }
}

In the update() ,

if(Input.GetKey(KeyCode.UpArrow) && _canClimb == true){
    transform.position = Vector3.Lerp(transform.position,ladderTop.transform.position,Time.deltaTime);
}

I have put a child game object to ladder to get the position of the top of the ladder.

But When the player jumps and hit with the box collider of the ladder I can see the climbing animation. And after that the player falls down to the ground again. It doesn't move up. What is the reason for that?

1 Answer 1

0

Now it is working as I expected, Here is the code,

if(Input.GetKey(KeyCode.UpArrow) && _canClimb == true){
        _myRigidBody.isKinematic = true;
        transform.position = Vector3.Lerp(transform.position,ladderTop.transform.position,Time.deltaTime);
}

I just added this line and it works perfectly :

_myRigidBody.isKinematic = true;

Rigidbody.isKinematic

If isKinematic is enabled, Forces, collisions or joints will not affect the rigidbody anymore.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.