'hello, i was recently working on my new game, and im getting a problem with *Infinite jumping*

so basically when I press jump/spacebar I jump but if I press spacebar continuously it just jumps and jumps and jumps and so on..., which I don't want I just want it to jump once.

code:

if (Input.GetKeyDown("space") && !isGrounded) 
    {
      velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
      isGrounded = true;
    }

let me know if you want to see all of the code, Thanks!.



Solution 1:[1]

Sounds like you may have a problem somewhere else in your code where isGrounded is being set to false when you don't want it to be. Have you tried doing a Raycast down from the bottom of the player, then setting isGrounded to true if that raycast is shorter than a certain length?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 obieFM