Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
August 30, 2022 06:24 am GMT

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

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

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!

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

And the actual code:

Updated movement code

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!


Original Link: https://dev.to/eduardojuliao/make-your-character-jump-in-unity2d-4jkc

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To