Hi! I’m working on a code that prevents the player spamming Jump, I came up with a script that should do just what I want but it seems that it works only every second time?? Note that in my game the jumps can differ in length, height and duration, that’s why I went with this approach. I would appreciate any help with my code or even ideas for a better one. Thanks!
//jumping based on current speed
if (controller.Grounded == true && Input.GetAxisRaw("Vertical") > 0 && afterjump == false)
{
player.AddForce(new Vector2(player.velocity.x - player.velocity.x * jumpdistance, jumpheight), ForceMode2D.Impulse);
Invoke("afterJumpFunction", 0.01f);
}
//finding time spot when player lands after jump
if (controller.Grounded == true && afterjump == true)
{
Invoke("jumpWait", jumpwait);
}
}
// Custom functions
void afterJumpFunction()
{
afterjump = true;
}
void jumpWait()
{
afterjump = false;
}
Note: jumpwait is just a float I’m setting in the inspector.