Give Your Character the Double Jump Ability in Unity
In order to double jump, we first need to single jump. We are going to do this by using Unity’s Character Controller component so let’s add that to our player object:
With the character controller component added, let’s create a “Player” script and add that to the player object.
We can open the new script and get access to our character controller component:
We need some gravity but we only want to check for gravity if the character isn’t grounded:
Let’s add a gravity variable, jumpHeight variable and check for the spacebar being pressed:
Here we set the _yVelocity to the _jumpHeight only if the space bar was pressed AND if the player is on the ground.
Once off the ground, we subtract _gravity from our _yVelocity. After that we set the velocity.y to the _yVelocity and then move our character giving us something like this:
Now for the double jump…In order to check if we have already jumped once, we can use a bool named “_canDoubleJump”. We can then set the bool to false, if we have double jumped and then return it to false once “.isGrounded” returns true:
Our complete code should look something like this: