2D Platformer: Jumping with Variable Heights

Eric Veciana
2 min readJul 4, 2023

--

Objective: Learning how to have the player jump depending on how long the space bar is pressed

While playing a platformer game, you may find various instances where it is important to choose how high you want to jump depending on how long the space key is pressed. That is what I will discuss in this article.

First, you want to have your basic physics components on your player, such as your collider and rigidbody.

Next, we are going to work within our Player script. We will use our GetKeyDown to tell the player to jump using physics.

Then, we want to use the GetKeyUp so we know when the player has potentially let go of the space key earlier than anticipated and I will use gravity to bring the player down quicker, because this felt like the natural way to approach physics.

Next, we want to make sure that when the player hits the space key to jump again, the gravity is set to 1 again.

Finally, whether we let go of the space key early or not, we still want our player to come back down from the jump using the extra forces of gravity. To do this, I will add an extra condition in the second if statement.

This extra condition basically states that the player is now coming downwards.

Now, when our player lightly taps the space key or once the player reaches the top of their jump, it will come down at a slightly faster rate which gives the player more control over the jump height.

--

--

Eric Veciana

As a Unity and C# developer with a background in nursing, I'm a creative problem-solver with a passion for creating games and other interactive experiences.