Spam Jumping

Hello! I made a Script for 3D Movement and I’m new, that brings a lot of problems, well only one for now. If I spam Jumping in the Game I wont be able to jump anymore does it have something to do with the code or the objects?

{

public float Movespeed = 5f;
private float Movejump = 5f;
private Rigidbody rb;
private bool grounded = true;

// Start is called before the first frame update
void Start()
{

    rb = GetComponent<Rigidbody>();

}


// Update is called once per frame
void Update()
{
 
    float horizontal = (Input.GetAxis("Horizontal") * Time.deltaTime * Movespeed);
    float vertical = (Input.GetAxis("Vertical") * Time.deltaTime * Movespeed);

    transform.Translate(horizontal, 0f, vertical);

    if (Input.GetButton("Jump") && grounded)
    {
        rb.AddForce(new Vector3(0, Movejump, 0), ForceMode.Impulse);
        grounded = false;
    }

    if (grounded == false)
    {

        Movejump = 0;

    }

    if (grounded == true)
    {
        Movejump = 5;
    }

}
private void OnCollisionEnter(Collision other)
{

    if(other.gameObject.tag == "Boden")
    {
        grounded = true;
        Debug.Log("Im working");
    }
}

}

tldr: Just remove 2 conditionals statements that changes MoveJump value.

If you hold Jump button for the time that your character jumps and falls down this will happen:

  1. Update() → button is down and grounded is true, so AddForce executes and grounded is set to false
  2. grounded is false so MoveJump is set to 0.
  3. you still hold your Jump button (GetButton(“Jump”) is still true)
  4. your character falls down
  5. OnCollisionEnter() → grounded is set to true
  6. Update() → GetButton(“Jump”) is still true and grounded is true, MoveJump has value of 0, so AddForce executes with 0 force, then grounded is set to false.
  7. you can’t jump anymore, because ‘grounded’ is false and you’re standing on the ground.

Fix: Just remove 2 conditionals statements that changes MoveJump value.

Check if your rigidbody is set to non-kinematic.