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.
And we have to create a brand new binding for the keyboard too.
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.
On our Replace()
, we have to inform that the Y velocity for our character physique.
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”.
As soon as created, transfer it barely down within the Y axis, -0.55 needs to be sufficient.
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!
Within the editor we will drag and drop the GroundCheck
sport object into our groundCheck
property within the participant controller 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...
.
On this new display screen, create a brand new layer known as Floor
.
We now have our Layer now, let’s change our floor sport object layer to be floor.
And with that, return to our participant enter controller, and alter the GroundLayer
to Floor
.
Is Participant Character grounded?
We now have every thing we’d like with the intention to make our participant leap correctly!
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:
And the precise 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!