So I’m building a platformer and right now I’m experimenting with jumping and movement. Everything looks good except one thing: sometimes when I hit the ground and press “jump” my character isn’t jumping. I’ve been looking into Hollow Knight jumping system and found out that in their game, if I hit the jump button even when I’m not on the ground, the player would still jump right after collision with it.
Somehow they managed to do that the player isn’t able to jump repeatedly while holding the jump button, but will jump on collision with ground even if you press “jump” buttor right before collision happens which creates smooth platforming experience. How to achieve the same result?
My jumping code:
void playerJump()
{
//Checking conditions for double jumping if player is in falling condition
if (isFalling && doubleJumpReady)
{
if (Input.GetButtonDown("Jump") && (currentJumpsValue > 0))
{
isJumping = true;
jumpTimeCounter = jumpTime;
doubleJumpReady = false;
}
if (Input.GetButton("Jump") && jumpTimeCounter > 0)
{
rdBody2D.velocity = new Vector2(rdBody2D.velocity.x, jumpForce);
jumpTimeCounter -= Time.deltaTime;
}
if (Input.GetButtonUp("Jump"))
{
isJumping = false;
jumpTimeCounter = 0;
currentJumpsValue--;
}
}
//Conditions for the first jump.
if (Input.GetButtonDown("Jump") && (currentJumpsValue > 0) && !isFalling)
{
isJumping = true;
jumpTimeCounter = jumpTime;
Debug.Log("Jumped!");
}
if (Input.GetButton("Jump") && jumpTimeCounter > 0)
{
rdBody2D.velocity = new Vector2(rdBody2D.velocity.x, jumpForce);
jumpTimeCounter -= Time.deltaTime;
}
if (Input.GetButtonUp("Jump"))
{
isJumping = false;
jumpTimeCounter = 0;
currentJumpsValue--;
}
}