Tuesday, August 30, 2022
HomeWordPress DevelopmentMake your character leap in Unity2D

Make your character leap in Unity2D


Now that now we have our participant character transferring from left to proper, we have to take a step ahead and make him leap.



Binding Leap motion to a button

We have to create a brand new binding into our PlayerInputController, this binding will likely be our Leap binding!
We’ll be creating extra bindings all through this tutorial.

Create the Jump binding - Gamepad

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

Create the Jump binding - Keyboard

As soon as created, do not forget to “Save Asset”, in any other case the binding is not going to be created.



Leap Code

Identical as velocity we’ll want a brand new property to inform us the leap pressure for our character.

jump force property

On our Replace(), we have to inform that the Y velocity for our character physique.

Initial jump code



Issues

Our character can leap! YAY! However he can leap and leap and leap as lengthy we preserve urgent the Leap button and we do not need that.

To repair that, we have to know if our character is touching the bottom earlier than leaping.



Is our character Grounded?

Let’s add a brand new sport object as a baby of our character. This will likely be our “floor checker”.

Player character ground checker

As soon as created, transfer it barely down within the Y axis, -0.55 needs to be sufficient.

Move ground check slightly down

Couple extra issues we have to add to our code:

  • Property telling if the participant is grounded or not.
  • A sport object property so the code is aware of what’s our floor verify object.
  • LayerMask so our code is aware of what’s a floor in our sport.

Let’s add them!

ground check properties

Within the editor we will drag and drop the GroundCheck sport object into our groundCheck property within the participant controller script.

Ground Check game object attached to the script

For the layer masks, we’ll must do some further work.



Making a Layer Masks

Go into the inspector (may be the participant object) and choose Layers, and Add Layer....

Add layer

On this new display screen, create a brand new layer known as Floor.

New Ground Layer

We now have our Layer now, let’s change our floor sport object layer to be floor.

Ground game object layer to ground

And with that, return to our participant enter controller, and alter the GroundLayer to Floor.

Ground Layer into the player script controller



Is Participant Character grounded?

We now have every thing we’d like with the intention to make our participant leap correctly!

Updated jump code

Utilizing Physics2D class from Unity, we’ll verify if our groundCheck is overlapping with one thing, on this case the bottom!

Run the sport and preserve urgent the leap button, we can’t be capable to leap greater than as soon as!



EXTRA!

It isn’t good to deal with physics (like transferring the rigidbody round) within the Replace methodology, so we’ll transfer a number of the code to the FixedUpdate() code, and depart the Replace() methodology to deal with the participant enter.

Some further properties:

Extra needed properties

And the precise code:

Updated movement code



Thanks

Thanks for studying! Hope this sequence is being useful and enjoyable for you as enjoyable is being writing them! See you within the subsequent chapter!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments