Jump buffering and Coyote time causes double jump when spamming jump
I implemented jump buffering and coyote time but when I spam the jump button, the player can do a double jump. How do I fix it?
//Ground Check
bool isGrounded;
float groundedRemember = 0;
float groundedRememberTime = .1f;
//Jump
public float jumpForce;
float jumpPressedRemember = 0;
float jumpPressedRememberTime = .2f;
How I code jump buffering and coyote time:
if ((jumpPressedRemember > 0) && (groundedRemember > 0))
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
jumpPressedRemember = 0;
groundedRemember = 0;
}
jumpPressedRemember -= Time.deltaTime;
if (Input.GetKeyDown(KeyCode.Space))
{
jumpPressedRemember = jumpPressedRememberTime;
}
groundedRemember -= Time.deltaTime;
if (isGrounded == true)
{
groundedRemember = groundedRememberTime;
}
Answer by Artik2442 · Jun 01, 2021 at 10:01 AM
I have only 2 explanations:
1) Your isGrounded bool doesn't change to false until you press the space bar a second time.
2) You pressed the space bar so fast that before isGrounded variable has changed, you had the time to double jump.
The most probable one is the first explanation. Set isGrounded to public to see in your inspector when it changes, if it changes correctly or not.
By the way, what do you use to check ground collision?
I checked and the isGrounded bool changes to false as soon as the player leaves the ground. I think the problem here is jumping will trigger the coyote time so after you jump, the script will still tell you you are on the ground and allow you to jump again. I use overlapbox to check ground collision.
Answer by GuirieSanchez · Nov 14, 2021 at 12:39 PM
Hi @ilovetoeatmeatballs , did you find the solution to that? It also happened to me when I set the jumpcooldown to a low number (something like 0.15 and below. So the problem might be what you've said: jump buffering and coyote time interacting with each other.
Found a fix but its not really the best, I created a Ienum and put a cooldown after the player jumps for the coyote time value + 0.01, put a boolean that must be true for the player to jump and set it to false until the timer finishes, once the timer finishes its goes back to true and the player can jump. Its not the best fix to be honest, but it works for now. I'll come back here if I ever find a better alternative.