3D coyote time

I want to add coyote time to my jumping. I am unsure how I would be able to do it without messing my existing code. I would like to do it by setting a timer when the player touches the ground and then reducing that with Time.deltaTime.

Here is my current jump code:

// Changes the height position of the player (jump)
        if (jumpAction.triggered && groundedPlayer)
        {
            playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
            animator.CrossFade(jumpAnimation, animationPlayTransition);
        }

        playerVelocity.y += gravityValue * Time.deltaTime;
        controller.Move(playerVelocity * Time.deltaTime);

I am unsure how I would be able to do it without messing my existing code.
You will have to mess with it, but you don’t need to mess it up! :slight_smile:

Basically, coyote time changes the condition for jumping from

I am touching the ground right now
to
I was touching the ground within the last X seconds

You can implement it like this:

private const float COYOTE_TIME_SECONDS = 0.5f;
private float timeSinceLastGroundTouch = Mathf.Infinity;
if (groundedPlayer)
{
    // Reset coyote time
    timeSinceLastGroundTouch = 0f;
}
else if (timeSinceLastGroundTouch < COYOTE_TIME_SECONDS)
{
    // "Run" coyote timer
    timeSinceLastGroundTouch += Time.deltaTime;
}

var canJump = false;
if (timeSinceLastGroundTouch < COYOTE_TIME_SECONDS)
{
    canJump = true;
}

if (jumpAction.triggered && canJump) // Note that it's canJump now
{
    // Jump
}