Recommand · June 11, 2021 0

Infinite Jump how to limit the Jump?

i’m using these script and the jump seems fine but the problem is i can do infinite Jump, how to limit the jump?

Like… i only want to jump once and jump again after hit the ground, not jump in the air, nor double jump…

Public void jumping()
    {
        if (GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName("Character - Idle - Nina"))
        {
            GetComponent<Animator>().SetTrigger("Jump");
        }

        if (GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName("Character - Run - Nina"))
        {
            GetComponent<Animator>().SetTrigger("Jump");
        }

        GetComponent<Rigidbody2D>().velocity = new Vector2(0, jump);

    }

You should define a boolean variable like canJump. when you collide with ground it should be true. When you press space for jump, if your canJump variable true, you can start jump anim then set your canJump variable false

bool canJump;

Public void jumping()
{
    if(canJump){

        if (GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName("Character - Idle - Nina"))
        {
            GetComponent<Animator>().SetTrigger("Jump");
        }

        if (GetComponent<Animator> 
 ().GetCurrentAnimatorStateInfo(0).IsName("Character - Run - Nina"))
        {
            GetComponent<Animator>().SetTrigger("Jump");
        }

        GetComponent<Rigidbody2D>().velocity = new Vector2(0, jump);


       canJump=false;
    }
}


void OnCollisionEnter(Collision collision){

    if(collision.gameObject.compareTag("ground")){
        canJump=true;
    }

}