How to add Double Jump to your Game
Using the Player class and CharacterController we worked with last time, we can add in functionality for a double jump fairly easily. The main adjustments needed will be a ‘_canDoubleJump’ private boolean (for us to tell when the player’s allowed to double jump) and some alterations to the current if statement in the Update method:
Whenever the player’s grounded, we’ll be setting the _canDoubleJump to true. As soon as they are no longer grounded, we’ll be permitting the player to double jump so long as that boolean remains true. If they press the ‘Jump’ button (spacebar or the A button on a controller), we’ll allow them to double jump by adding an additional _jumpHeight float to their current _yVelocity before setting _canDoubleJump to false.
We might need to make a few more adjustments to this and some of the jumping/gravity logic at some point, but for the time being this should give us the functionality we want 😉