Tap & Hold with the New Unity Input System

Heathrileyo
4 min readAug 19, 2022

--

Hello Hello! Today we’re going to do a fun little exercise using the new Unity input system and some scripting. We’re going to make a ball bounce up a little bit if we hold a key for less than a second, and then if we hold it for more than a second it will automatically bounce up a lot! We have all the tools we need already so let’s jump in. First we’ll setup our scene.

We’ve got our ball sitting on a plane. We’re now going into our assets and making a new input asset, action map for the ball, and an action called bounce that requires the space key to be held for 1 second.

Once that’s done let’s turn the New Controls into a C# script. We’ll make another script simply called BallBounce and attach it to the sphere in the scene.

All right! Let’s get things initiated in our BallBounce script. Can you remember everything we need to setup first? We have to get a reference to the NewControls, make an instance, and enable the action map. This time we’re going to make TWO different cases for our Bounce action. One for when a tap of the space key was lifted up before the full second passed (canceled), and one for when the space key was pressed and held down for a full second (performed).

Before we make these cases, let’s add a rigid body to our sphere since Unity might not initially add that component and we’re going to need it.

Okay, we’ll start with the full jump first and this will be the performed scenario. When the full press of 1 second is detected we want a force to be applied to the ball to make it jump up and then fall back down. So let’s use AddForce on the rigid body.

That looks great! Let’s jump over into our scene and press play. Then we’ll hold the space key down for 1 second to watch the ball launch up and fall back down just like we programmed. At this point there is no partial jump. We’ll make that next.

Looking good! Time for a partial jump. This will be our canceled scenario, where the key was lifted before 1 full second passed. How can we go about making a smaller jump? We can use the duration of the press up to 1 second as a multiplier for the same jump formula. So we want to make a variable for our obj.duration (Changing obj to “context” is completely fine also). We also want to make sure this doesn’t happen AFTER a full jump so let’s make sure that if the duration goes past 1 second we set obj.duration to 0. We could easily add in a double jump feature later by adjusting this.

We made our variable for the duration, we made sure it was less than one, then we multiplied it to our original jump formula. Since it will be less than one, it will be a fraction and make our jumps proportional to the amount of time held. Let’s jump back to our scene and press the space key a couple times under 1 second.

Perfect! You can see THREE different jump heights all with the same code, just different lengths of time holding on the key. You can do a lot with this and many games will use this feature with something like a power gauge to see how hard you’ll hit an object. I hope this was a fun exercise and I’ll see you again soon!

--

--

Heathrileyo

Full time father of two attempts to fill your prescription and become proficient in the world of game dev