DEV Community

Eduardo Julião
Eduardo Julião

Posted on • Updated on

Make your character jump in Unity2D

Now that we have our player character moving from left to right, we need to take a step forward and make him jump.

Binding Jump action to a button

We need to create a new binding into our PlayerInputController, this binding will be our Jump binding!
We'll be creating more bindings throughout this tutorial.

Create the Jump binding - Gamepad

And we need to create a new binding for the keyboard too.

Create the Jump binding - Keyboard

Once created, don't forget to "Save Asset", otherwise the binding will not be created.

Jump Code

Same as speed we will need a new property to tell us the jump force for our character.

jump force property

On our Update(), we need to tell that the Y velocity for our character body.

Initial jump code


Our character can jump! YAY! But he can jump and jump and jump as long we keep pressing the Jump button and we don't want that.

To fix that, we need to know if our character is touching the ground before jumping.

Is our character Grounded?

Let's add a new game object as a child of our character. This will be our "ground checker".

Player character ground checker

Once created, move it slightly down in the Y axis, -0.55 should be enough.

Move ground check slightly down

Couple more things we need to add to our code:

  • Property telling if the player is grounded or not.
  • A game object property so the code knows what is our ground check object.
  • LayerMask so our code knows what is a ground in our game.

Let's add them!

ground check properties

In the editor we're going to drag and drop the GroundCheck game object into our groundCheck property in the player controller script.

Ground Check game object attached to the script

For the layer mask, we'll need to do some extra work.

Creating a Layer Mask

Go into the inspector (can be the player object) and select Layers, and Add Layer....

Add layer

In this new screen, create a new layer called Ground.

New Ground Layer

We have our Layer now, let's change our ground game object layer to be ground.

Ground game object layer to ground

And with that, go back to our player input controller, and change the GroundLayer to Ground.

Ground Layer into the player script controller

Is Player Character grounded?

We have everything we need in order to make our player jump properly!

Updated jump code

Using Physics2D class from Unity, we'll check if our groundCheck is overlapping with something, in this case the ground!

Run the game and keep pressing the jump button, we won't be able to jump more than once!


It's not good to handle physics (like moving the rigidbody around) in the Update method, so we'll move some of the code to the FixedUpdate() code, and leave the Update() method to handle the player input.

Some extra properties:

Extra needed properties

And the actual code:

Image description


Thanks for reading! Hope this series is being helpful and fun for you as fun is being writing them! See you in the next chapter!


Bug fixes

Fixed a bug in the code where you can press space bar multiple times and would trigger a second jump when hitting the ground.

Why it happened?

When pressing space bar multiple times, the code would just set hasJumpButtonTriggered to true in mid air, making a jump action valid and thus, jumping again once hitting the ground.

The fix

The fix was pretty easy, moving the isGrounded check to Update where the code is checking if it's ground, with this change, hasJumpButtonTriggered can only be changed to true once the jump key is pressed AND it's grounded

Top comments (2)

farina7 profile image

Hi small thing, I found a small problem in the code.
The first if should check if you're also on the ground with isGrounded.

If there isn't this check you can press the button for jumping anytime during the jump, and the player would jump immediately after touching the ground creating a strange second jump.

eduardojuliao profile image
Eduardo Julião

Hi @farina7 !

I’ll have a look and update this post with the fix! Thanks for pointing it out and sorry the late response 😅