Working with Unity New Input System
Making a Bouncy Ball with different variations of jump strength
As a next challenge we will be task to create an bouncy call that can jump while pressing the Space key. Depending on how long we press the space key the higher the ball can jump.
First we are going prep the scene so that we can have everything setup before the scripting can start. We will begin by created an sphere 3D object that will represent our bouncy ball and then a 3D plane which will the floor.
The Bouncy call will also have a “Rigidbody” component so the ball will have gravity applied to it.
Then in the Action Input, we are going to create an Action Map for this bouncy ball. The only action binding we current need would be a Jump button linked to the Space button.
If we were to break down what is required for the Jump button via logic, we basically want the Jump button to have more jumping force the longer we hold down the button. We also want the button to preform a regular jump when we are just tapping the button.
Therefore we will need a “Hold” interaction for the Jump Button.
I made the “Hold” settings Hold Time to 1 whole second instead of Default. Make sure to save the Input Actions before starting to script.
We start by making our Input Actions into a C# script (if you haven’t done so already)
Then we are going to create an new C# script called “Bouncing” and we will attached this new script into our Bouncy call game object.
Inside the script we are going to do the basic requirements to gain access to the Input Library and also initializing the Player Inputs.
To fully understand how we are going to apply the hold and tap interactions into our script, we have to understand how to we are going use the buttons work.
>When we press and hold the space button to jump for 1 second = Maxmium Jump.
>When we press and release the the space button to jump between the 0.1 sescond to 0.5 seconds = Regular Jump.
If we decide to assign the “performed” function as the Maximum Jump setting, then basically anytime we release the button before the 1 whole second has bee completed then that would be considered a “Cancel” function.
performed = Max Jump
canceled = Regular Jump
So for the Jump Performed method we are going to do the maximum jump.
For the Canceled method, it is going to be a tad more complicated. That is because we will need to know the duration the button has been press down on. To calculate that, Unity has a function called “duration”.
Once we know the duration we can convert that unit into a variable which can be applied to our jumping equation. Just like how we did the math for the Max Jump we are going to do the same equation for the canceled jump, the only difference is that we are going to multiply the duration to the jump force variable. Then we have to convert the entire unit into a “Float”. This must be done otherwise there will be an error.
Once you finished, head back to Unity and within the script component where we place in ball object, we will need to input the jump force variable.
That way, your jump formula will have an proper unit to execute the equation.