Unity new Input System : Jump (Part VIII)
In this article we look at the jump, based on how long we hold the button down.
It can be useful in many situation, so, let’s start it!
I don’t reapeat every step, but what I do here is :
I create a Input Action, I create new Action called Jump, I set Action type to Button, and I use Space Bar as Key Binding.
Then, Interaction -> Hold, and I set Hold Time to 1 (second).
In PlayerInputActions I toogle Generate C# Class and press apply
This is the result :
This is for Input.
In the scene I create a Plane, at position 0, 0, 0
and I add a Sphere.
The Sphere is at position 0, 0.5f, 0, and I add the Rigidbody component
As you can see, I have even Player script added to the Sphere.
Now we have everything set up, go to the code part.
At the beginning we need a reference to RigidBody and PlayerInputActions, and in Start we initialize them.
And as always we create the Jump_performed method.
Inside I use the AddForce method.
Inside it we pass the Vector3.up multiply for some “force” (I use 15f, feel free to experiment), and I use the ForceMode.Impulse.
With this implementation, we can hold the button to Jump.
But this is not the mechanic we want, we want the “jump force” is based on how long we hold the Space Bar.
This part is fine, when we hold Space bar for a second, we Jump at maximum force.
To check how long we hold the button down, we can use the
Now, I can be wrong, but I think canceled is really similar to GetKeyUp in the old input system.
If we make a test,
Every time I left the Space Bar, the console print.
I’m not sure at 100%, but it seems work at the same way.
Now, why we can use this?
The logic behind is :
if we calculate the difference between when I press and when I release, and multiply for jump Force, we can jump base on that time right?
But how can we calculate that time?
Well, the documentation comes to the rescue!
In this section we have one property called duration
As we can see, duration is a double and
calculate the the difference between time and startTime.
Technically, in this method, we need to store this kind of information :
or we can make a little jump, or we are in a full jump because we hold down Space Bar for one second, so we don’t do nothing.
The easiest wey to store that info, is a boolean.
And this boolean becoming true when we are in full jump right?
so we put in here
Back to duration property.
In the Jump Canceled method, we need that type of information, because if we release BEFORE 1 second, we want to jump based on that lass of time.
So we can access to context.duration, and store it in a variable.
Now, if we MULTIPLY that VALUE, to our force, we obtain exactly what we want : more we hold, more we jump.
Two little notes, we need to do an explicit cast on forceEffect as FLOAT, because is a DOUBLE.
And everything is inside an If Statement,
if we jumped == false do light jump
If we try it now (I change 15 to 10, full jump at 15 is really really high)
And now we have a jump based on how long we hold the button down.
See you in the next articles!