Member-only story
How to add Double Jump to your Game
Using the character controller.
Double jump is actually a really easy thing to add to your player. All we need is a Boolean variable and the some if…else statements and you have a working double jump.
[SerializeField] private bool _canDoubleJump;
I set the above variable as a class variable but we could make it a local variable as well. I like to have it serialized for testing purposes. So what does our jump code do.
if (Input.GetButtonDown("Jump") && _groundedPlayer)
{
_velocity.y = Mathf.Sqrt((jumpHeight * -3.0f * gravityValue));
_canDoubleJump = true;
}
else
{
if (Input.GetButtonDown("Jump") && _canDoubleJump)
{
_velocity.y = Mathf.Sqrt((jumpHeight * -3.0f * gravity));
_canDoubleJump = false;
}
_velocity.y += gravity * Time.deltaTime;
}
The jump code is pretty straight forward. The first If checks if the player can make an initial jump from the ground, when they do they are give the ability to jump a second time, if the player pushes the jump button again before they hit the ground they can jump a second time.
From here we can fine tune how the double jump behaves.