Double Jumping Character Controller

Here I will demonstrate how to add a double jumping mechanic to our character controller.

Here is our jump code before we add the double jump capability:

Basically the character can only jump if they are on the ground. We want to extend the above code to allow the player to also jump one more time if they are in the air.

In order to add a double jump we modify the code like so:

A couple things to point out. First the player can only do the double jump if they are already in the air. So first we check to see if the player is grounded. If they push the space bar while grounded we will set the y component of their velocity to jump height and we will enable the double jump ability with _canDoubleJump = true;

If the player is already in the air and has not yet double jumped then they can double jump if they press the space bar. To accomplish this we add the jump height to the y component of velocity, which will propel the player upward for the double jump. We then set _canDoubleJump to false as we only want one double jump at a time. It will be set back to true when the player lands and then performs another jump.

Here is the final product after making the code additions:

--

--

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
Jon Jenkins

Jon Jenkins

A Unity Developer, interested in all things Unity.