Home > Net >  Player jumps when it touches the ground but double jumps if I dont use my jump
Player jumps when it touches the ground but double jumps if I dont use my jump

Time:10-07

enter image description here

[...]
    // jump
    float verticleMove = joyStick.Vertical;
    if (verticleMove >= .1f && isGrounded)
    {
        body.AddForce(new Vector2(0, 10), ForceMode2D.Impulse);
        FindObjectOfType<AudioManager>().Play("Jump");
        isGrounded = false;
    }
}

void OnCollisionEnter2D(Collision2D collision)
{
    if (collision.collider.CompareTag("Enemy"))
    {
        TakeDamage(50);
    }
    if (collision.collider.CompareTag("Ground"))
    {
        isGrounded = true;
    }
}

If I jump and land on ground, and after that if I dont use jump, e.g if I'm falling off the platform I can use that jump. Why does it get stacked? I want to use my jump only when Im touching the ground. I have attached the screen shot of the code for jumping as well.

CodePudding user response:

Once you enable your jumping with isGrounded = true;, you can use it once again. If you want to disable for example when you are falling off the platform, I would try:

void OnCollisionExit2D(Collision other)
{
    if (collision.collider.CompareTag("Ground"))
    {
        isGrounded = false;
    }
}

So that the jumping also gets disabled when you are not touching the ground, but not only for the case when you have jumped but for any other.

  • Related