- Home /
Coyote Time/Ledge Assistance Causes Double Jump
Hello! Thank you for reading! If you don't know coyote time/ledge assistance is when the player has a short amount of time to jump after leaving the platform they are on. This is how I am doing it:
First I define a float called groundedRemember.
public float groundedRemember = 0;
Then in Update() I subtract Time.deltaTime from it.
groundedRemember -= Time.deltaTime;
Also in Update() I check if the player is grounded, if so, I set groundedRemember to 0.25.
if (grounded)
{
groundedRemember = 0.25f;
}
Then if the player jumps and all the conditions are true (jumpPressRemember is for a jump buffer, and topChecker is to make sure there is nothing above the player) then make the player jump, making sure to reset the groundedRemember timer.
if ((jumpPressRemember > 0) && groundedRemember > 0 && !topChecker.GetComponent<TopChecker>().touching)
{
jumpPressRemember = 0;
groundedRemember = 0;
Debug.Log("Jamp");
playerRb.AddForce(Vector2.up * jumpSpeed, ForceMode2D.Impulse);
jumpSound.Play();
}
The problem is if the player spams jump they are double jump for a split second. I thought resetting the timer would fix this but it does not. Why is this occurring? I can upload more details if necessary. Thank you again! Any help is appreciated!
Answer by Epicnez · May 19, 2020 at 01:03 AM
@unity_ek98vnTRplGj8Q Thank you for the code snippet, the only thing that had to be changed in order to work was in the last if statement: if(jumpTimer > 0 && blah blah blah){ jumpTimer = jumpCooldown; //All your other stuff } The part that says jumpTimer > 0, should be groundedRemember > 0, or else the player will not be able to able to jump at all. Thank you so much again! It worked perfectly!
Answer by unity_ek98vnTRplGj8Q · May 18, 2020 at 08:57 PM
My guess is that you are setting the groundedRemember to 0 during the initial jump, but then the following Update frame your player has moved so little that it is still detected as being grounded and groundedRemember gets set straight back to 0.25. This will depend on a lot of things, like framerate and how exactly you are checking if the character is grounded. I recommend adding a short jump cooldown (during which groundedRemember is not set to 0.25).
@unity_ek98vnTRplGj8Q I am checking if the player is grounded using this line of code:
grounded = Physics2D.OverlapBox(playerFeet.transform.position, new Vector2(.72f, .16f), 0, groundLayers);
If I replace this:
if (grounded)
{
groundedRemember = 0.25f;
}
With this:
if (grounded)
{
StartCoroutine(GroundedCoolDown());
}
public IEnumerator GroundedCoolDown()
{
yield return new WaitForSeconds(.1f);
groundedRemember = 0.25f;
}
Unfortunately this does not fix the problem. It seems that the player can double jump for 0.25 seconds after jumping, I think this because that is the amount the groundedRemember float is set to. Thank you for your help!
You are still setting groundedRemember to 0.25 the next frame, this code just delays it by a tenth of a second. I was thinking of something more like this
public float jumpCooldown = 0.1f;
float jumpTimer = 0;
void Update () {
if(jumpTimer > 0) jumpTimer -= Time.deltaTime;
if (grounded && jumpTimer <= 0) {
groundedRemember = 0.25f;
}
if(jumpTimer > 0 && blah blah blah){
jumpTimer = jumpCooldown;
//All your other stuff
}
}
Answer by Epicnez · May 19, 2020 at 01:03 AM
@unity_ek98vnTRplGj8Q Thank you for the code snippet, the only thing that had to be changed in order to work was in the last if statement: if(jumpTimer > 0 && blah blah blah){ jumpTimer = jumpCooldown; //All your other stuff } The part that says jumpTimer > 0, should be groundedRemember > 0, or else the player will not be able to able to jump at all. Thank you so much again! It worked perfectly!
Answer by adscomics · Sep 23, 2020 at 05:35 AM
Just wanted to say that this helped me a TON. I was tearing my hair out figuring out how to solve this issue. Thanks @unity_ek98vnTRplGj8Q for answering the question, and thank you @Epicnez for asking it! I was worried this issue would be really niche.