My journey becoming a Unity game developer: 2.5D Infinite Runner-Regular jump to double jumping
Objective: Allow the player to do a regular jump or a double jump to the different platforms in the game.
To set the player up to jump, let’s start with creating a variable of Type Float name jumpHeight and set it to 15f to represent the meters the player will be moving in. Now if the player which is the controller isGrounded and the user presses the Spacebar, we want the player to move with Velocity along the Y-axis to equal the jumpHeight.
We will see the player now jumping at 15 meters per second in the air on the Y-axis.
However, the player jumping is glitchy because after the player jumps, the next frame in Update() is going through the code all over again. This means the velocity.y which is set to 15f will be set back to zero because of the direction variable resetting the Y-axis to zero. Plus, the velocity variable which multiplies direction times speed will end up with the Y-axis and Z-axis equaling zero before coming back to the isGrounded IF condition.
Therefore, we will create a new variable name yVelocity with a Type Float to be used to set the Y-axis position to the jumpHeight value when the player jumps. Replace the velocity.y when the user presses the Spacebar, and when gravity is being applied. The other thing we must do is set the velocity.y to equal the yVelocity after the conditions are met.
Now the player is jumping more freely, but this isn’t the proper way the player should be jumping in this GIF. The recording of this jumping action is allowing the player to jump beyond the 15 meter restriction, but when we play the game without recording, the player jumps only the 15 meters set.
Final thing we want to do is allow the player to do a double jump in the game. Create another new variable Type Bool name canDoubleJump, and set its value to False.
Inside the Update() function, instead of checking IF the player isGrounded first and IF the user pressed the Spacebar second, we’re going to Reverse the order. Check IF the Spacebar has been pressed first, then check IF the player isGrounded second as this will allow the player to do the double jump properly.
After the player jumps for the 1st time IF their grounded, we must set the canDoubleJump to equal True which will allow the player the chance to double jump if needed. Then IF the user presses the Spacebar and canDoubleJump is True, then use the yVelocity’s value to add to the jumpHeight which will create the actual double jump. Also, we need to set the player to not be able to jump anymore until they touch the ground by setting the canDoubleJump to equal False.
As we see, the player now can double jump in the game. Please excuse the accuracy of these jumps, but recording this GIF is allowing the player to exceed its limits sometimes.
Play around with the gravity and jumpHeight values to get the perfect height for the player’s jump according to your scene’s layout. Next time we will work on the player being able to Collect Coins.