Day 77 — Adding in the Ability to Jump and Double Jump
Hey and welcome!
Now that we have our movement and gravity in place it’s time to implement jumping into our game, wouldn’t be much of a platformer if you couldn’t jump! Although now I want to see if you could make a platformer without jumping, a project for a different day.
Let’s start things off by looking at the code to jump normally first:
private float yVelocity;
private float _jumpHeight = 15.0fif (_playerController.isGrounded == true)
{
if (Input.GetKeyDown(KeyCode.Space))
{
_yVelocity = _jumpHeight; if (Input.GetKeyDown(KeyCode.Space))
{
_yVelocity = _jumpHeight;
}
}
}else
{
_yVelocity -= _gravity;
}velocity.y = _yVelocity;
Alright let’s talk about the elephant in the code here, you may notice that i’m caching the y velocity by using yVelocity instead of just assigning the _jumpHeight directly to the velocity.y like we did for you our gravity before. If you do happen to try that out you would get something like this happen:
Not a very practical jump there. The reason that happens is because the code in Update() runs every frame, so in the first frame we assign the value of 15.0f to velocity.y but the problem happens in the very next frame because we’re assigning direction back onto velocity what makes the y velocity 0. Since the player is no longer grounded it doesn’t run the code that assigns the jump height to the velocity so our velocity is going from 15 to 0 nearly instantly.
To get around that we create a separate variable to store the current y velocity in and then take the gravity away from that while assigning the yVelocity back onto velocity.y right after. So if the gravity is 1.0f then the value of yVelocity is decreasing by 1 every frame until the player collides with a platform, giving a better looking jump with a more gradual decline.
I recommend playing about with the gravity and jump height values if you’re coming across any issues here.
Now let’s look at what’s needed to make the player double jump:
private bool _canDoubleJump = false;if (_playerController.isGrounded == true)
{
if (Input.GetKeyDown(KeyCode.Space))
{
_yVelocity = _jumpHeight; if (Input.GetKeyDown(KeyCode.Space))
{
_yVelocity = _jumpHeight;
_canDoubleJump = true;
}
}
}else
{
if (Input.GetKeyDown(KeyCode.Space))
{
if (_canDoubleJump == true)
{
_yVelocity += _jumpHeight;
_canDoubleJump = false;
}
}
_yVelocity -= _gravity;
}
The bit highlighted in bold here is the new code getting added in to make the double jump work. Basically we’re creating a bool called _canDoubleJump letting the code know whether the option to double jump is doable or not. If it is then we add on and assign _jumpHeight onto whatever the current value that _jumpHeight is in order to get the effect we want.
That’s everything for the jumping for now, this is something that I want to come back to since I think the jumping as it is now is affected by different frame rates so I’ll want to test some things out with that, but for now I’d say it’s working pretty well!