Make your character jump in Unity2D
Tags: beginners, gamedev, programming, tutorial
Published: 2022-08-30 06:24:24

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jtyvwiuezdhttr8qcd94.png)

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


![Create the Jump binding - Keyboard](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tpp69az1dly9s76jyt4x.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mc4mi91j8xhq3zhesd48.png)

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

![Initial jump code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hssleh3np85mdcs77mvx.png)

### Problems
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/06e0vccd9w9qs55q58x3.png)

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

![Move ground check slightly down](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/owpyhboil2e0vbmlswsy.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vvj7x3ez1i7omt13blje.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p21t60u5nllg5mrer0ov.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/km8w60woi7a785sqfgx5.png)

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

![New Ground Layer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s66ne5lc7lig7yl0atf4.png)

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

![Ground game object layer to ground](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lex2ncvy72ei7wwgtpm7.png)

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

![Ground Layer into the player script controller](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2n6huudyfu6rfslawoxk.png)

## Is Player Character grounded?

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

![Updated jump code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nz9ms2if88j3ruqf451f.png)

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!

## EXTRA!

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bytb8symsbwew3if33zv.png)

And the actual code:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zgq83nupop85awkvsi23.png)

## Thanks

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!

## Edit

### 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