Double jump script not working

I am trying to figure out why this is not working. The issue is that the “DoubleJumpUnlocked && Input.GetKeyDown(KeyCode.W) && canDoubleJump” seems to only become true during one frame after pressing the ‘W’ button. Assume the DoubleJumpUnlocked is true and the canDoubleJump is true when I jump.

if (isGrounded())
            canDoubleJump = false;
        Debug.Log("Can Jump: " + canDoubleJump);// + " | Unlocked: " + DoubleJumpUnlocked + " | Is it working: " + (DoubleJumpUnlocked && Input.GetKeyDown(KeyCode.W) && canDoubleJump));

        if (DoubleJumpUnlocked && Input.GetKeyDown(KeyCode.W) && canDoubleJump)
        {
            Debug.Log("Double Jumped.");
            playerRigid.velocity = new Vector2(0, jumpForce / 2);
            canDoubleJump = false;
        }
        if (isGrounded() && Input.GetKey(KeyCode.W))
        {
            playerRigid.velocity = new Vector2(0, jumpForce);
            canDoubleJump = true;
        }

If canDoubleJump is being reset to false, then the error would be in your isGrounded() check since the next frame of what I assume is the Update() method checks for isGrounded() and is probably setting it back to false.

Edit
@kdgalla post after mine suggest maybe I don’t understand the implementation. I assume for double jump it’s tap w to jump once, tap again while in the air for the second jump. I’m not sure if the intention is to allow holding down the w key to have different jump heights.

Yeah, GetKeyDown is only true for one frame after pressing the key. If you want to know whether the use is currently holding the key down, then use GetKey instead of GetKeyDown.

1 Like

I’m wanting to double jump after I jump once and am still in the air.