Player double jumping instead of jumping just once

I’ve been making a game based off the Roll a Ball Tutorial to learn unity and scripting. I’ve added jumping to the game using a bool isGrounded to check whether or not the player is in contact with the ground or not, it all seems to work except if I double tap the spacebar key fast enough he double jumps instead of just jumping once.

Here are the parts of my script for the player object:

public float speed;
private Rigidbody rb;
public float jumpheight;
public bool isGrounded;

void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

void OnCollisionStay()
	{
		isGrounded = true;
	}

void FixedUpdate ()
	{
		if (Input.GetKeyDown ("space") && isGrounded)
		{
			
			Vector3 jump = new Vector3 (0.0f, jumpheight, 0.0f);
			GetComponent<Rigidbody> ().AddForce (jump);
			isGrounded = false;
		}
		float moveHorizontal = Input.GetAxis ("Horizontal");
		float moveVertical = Input.GetAxis ("Vertical");

		Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);

        rb.AddForce (movement * speed);
	}

two time addForce used , GetComponent ().AddForce (jump); will make jump once and rb.AddForce (movement * speed); will do 2nd time jump

2 Answers

2

try like this:

 public float speed;
 private Rigidbody rb;
 public float jumpheight;
 public bool isGrounded;
 int pad;
 void Start()
     {
         rb = GetComponent<Rigidbody>();
     }
 
 void OnCollisionStay()
     {
         isGrounded = true;
     }
 
 void FixedUpdate ()
     {if(pad>0){pad--;  isGrounded = false;}
         if (Input.GetKeyDown ("space") && isGrounded)
         {if(pad==0){
             pad=3;//<adjust this number
             Vector3 jump = new Vector3 (0.0f, jumpheight, 0.0f);
             GetComponent<Rigidbody> ().AddForce (jump);
             isGrounded = false;
         }}
         float moveHorizontal = Input.GetAxis ("Horizontal");
         float moveVertical = Input.GetAxis ("Vertical");
 
         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
 
         rb.AddForce (movement * speed);
     }

Could you or someone else explain how the pad integer is stopping my player from double jumping? It's over my head right now and if someone could explain it that would be extremely helpful. I ended up using this solution and also having the function OnCollisionExit setting isGrounded to False because the player could jump up walls repeatedly. Sadly he can still do that but it's really hard to reproduce.

Works great for the double jumping problem I've been hunting down for a while! Thnx.

Edited, check now. I have removed "void" from IEnumerator decalaration. Haha, that's the beauty of writing in notepad++

I think that the way that you’ve coded it, the variable ‘isGrounded’ will become true when the player hits the ground, but nothing in your script sets it to false again when the player jumps.
OnCollisionStay() is called on the frame that the player hits the ground and every frame after
OnCollisionEnter() is called only on the frame that the player hits the ground
OnCollisionExit() is called on the frame that the player leaves the ground

so instead of:

void OnCollisionStay()
     {
         isGrounded = true;
     }

try:

void OnCollisionEnter()
{
    isGrounded = true;
}

void OnCollisionExit()
{
    isGrounded = false;
}

I think this will work, but i could be wrong. Let me know :slight_smile:

Also in your code you wrote GetComponent<Rigidbody> ().AddForce (jump); When you should use rb.AddForce (jump); So that it doesn't have to Get the rigid body component every time. I'm guessing that was just a mistake though.

@TacoMakerMan This worked as well thank you btw!