- Home /
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);
Comment
Answer by FlaSh-G · Sep 11, 2021 at 01:17 PM
You will have to mess with it, but you don't need to mess it up! :) Basically, coyote time changes the condition for jumping fromI am unsure how I would be able to do it without messing my existing code.
toI am touching the ground right now
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
}
Your answer
Follow this Question
Related Questions
Can't figure out how to detect ground (2D) 1 Answer
Revise one conflicting code line? 1 Answer
How to make a ball jump? 2 Answers
,How to disable autojumping when holding spacebar? 4 Answers
3D Game-Making a character jump 3 Answers