A Physics Based Character Controller for Unity — Part 2 Jumping

Single and double jump implemented

Creating the variables in the player script

So far we simple used a var velocity defined by the direction and speed, but as its y value gets reset each time the update method runs, we are going to add an additional variable to cache y value of velocity: _yVelocity.

private float _yVelocity;
private float _jumpHeight = 25.0f;
private bool _jumpAgain = false;

Implementing the single jump

From the last article we already have an if else statement checking whether the player isGrounded or not. Here is what it looks like

if(_controller.isGrounded == true){
//do nothing for now
}
else{
velocity.y -= _gravity;
}
_yVelocity -= _gravity

Implementing the double jump

Feature: — When the player has jumped once and the space bar is pressed, he will jump again — and thereby double jump. The player can only double jump once after each single jump.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store