I need help disabling double jump in my game

Updated on January 12, 2019 in Unity
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
2 on January 11, 2019

I’m trying to stop my player from double jumping. I’ve written my code to check if whether or not the player has collided with the ground. My code works if I’m using a keyboard button with my ground collision check, but when I switch my code to use a sprite button with the same ground collision check, it doesn’t work the player will be allowed to jump when I click my button the player can jump an infinite number of times.Literally jumping over the level. When I build my game to my phone its the same thing of course. 

See my code below. I appreciate any and all assistance. Thanks!  

 

 

[

if (Input.GetButtonDown(“Jump”) && isGrounded == true)
{
Jump();
isGrounded = false;
}

 

public void Jump()
{

rb.AddForce(0, jumpForce * Time.deltaTime, 0, ForceMode.VelocityChange);

}

void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == “Ground”)
{
isGrounded = true;

}

/]

 

  • Liked by
Reply
1 on January 11, 2019

On your UI button, does it just call the Jump() method?

If that is the case, then it’s because you are only checking if they are grounded when they use the keyboard.

Try this for your Jump() method:

public void Jump()
{
     if(isGrounded == false) return;
     rb.AddForce(0, jumpForce * Time.deltaTime, 0, ForceMode.VelocityChange);
     isGrounded = false;
}

Now, no matter how you call Jump(), it checks itself to see if the player is on the ground.

 

edit: set isGrounded to false in jump too since it isn’t being done anywhere else.

on January 12, 2019

Yes my UI button calls my jump method.

Show more replies
  • Liked by
Reply
Cancel