Double Jump
Last updated
Last updated
This tutorial explores how to add double jump to a platformer character. It covers how to add an extra jump (double jump), infinite jumping, or a limited number of jumps in the air.
Double jumping is a feature in many platforms which gives the player more control over player movement. Players can use double jumping to remain in the air for a longer period of time, to reach areas higher than possible with a single jump, and improve horizontal movement precision. Entities which support double jumping require two sets of values - the movement values before double jump and the movement values to apply after double jump. Platformer entities automatically receive a platformer values called Air which are the before double jump variables, so we need to create a new set of values. To do this:
Select your platformer entity (Player)
Expand the drop-down next to the Add Movement Type button
Select Default Air Control option
Change the name of the new values to AfterDoubleJump
Now that we have a set of values for after double jump, we need to tell our game to use these new values for double jump:
Select the platformer entity
Click the Variables tab
Change the After Double Jump value dropdown to the name of the new set of values we just created (AfterDoubleJump)
Finally we need to change the Jump Speed value on the Air movement values to be greater than zero. This is the velocity which will be applied when performing a double jump.
Now the platformer entity (Player) supports double jumping.
We can also support infinite double jumps by either setting the AfterDoubleJump Jump Speed value to greater than zero, or by setting the AfterDoubleJump variable to be Air. This results in the character being able to jump indefinitely which can be useful if implementing swimming or abilities like the flying racoon power-up in Super Mario Bros 3.
As shown above, the platformer entity can support a single double jump by making the AfterDoubleJump variables have a Jump Speed of 0. By increasing the Jump Speed to greater than 0, then the platformer entity can jump infinitely similar to flying or swimming. It is also possible to limit the number of jumps, but this requires custom code.
For this example we will use the two movement types from the previous sections: Air and AfterDoubleJump. To limit the number of jumps, make sure that AfterDoubleJump has a Jump Speed value of 0.
Next we will conditionally assign the Air value in code AirMovement
value in code according to the number of jumps the player has performed since touching the ground.
To do so, open your platformer entity's code file (Player.cs) and modify the code as shown in the following snippet:
Now our platformer entity can jump 5 times, as controlled by a variable in HandleJump.