Unity’s New Input System: Press and Hold Interaction

Marcus Cohee
2 min readAug 15, 2022

--

Hello all!

Today, I will be going over how to make a ball charge a bounce using a hold interaction!

Lower left: Shows how long the space key was pressed.

Prepping the Scene

  1. Make a floor
  2. Make a sphere
  3. Give the sphere a rigidbody component
  4. Create a Player C# script and assign it to the sphere
  5. Add a new Input Actions asset

Setting up your Input Action Map

  1. Create a Player Action Map.
  2. A jump or bounce action with a button action type.
  3. Add a binding with a path to the Space key.
  4. Add a new Hold Interaction and set the Hold Time to 1 second.
  5. Generate the C# Class

Making the C# Script

First, grab the references to your input and rigidbody from the sphere. Then enable the action map and we need to get access to the information when the action Bouncing is either performed or canceled.

Performed:

When Space is held for the full 1 second

When you hold the space key for the whole duration, the sphere will be launched up at full charge(_bounce).

Canceled:

When Space is let go early.

During a hold interaction, there is a timer that starts as soon as you hit the desired key. You can get that timer’s value if you use obj.duration. That means you can use that value kind of like a “percent of charge” by multiplying it to _bounce. Thus, making a smaller bounce if you release before full charge!

That was how to add a charging action!

Next up, I’ll go over how the new input system helps with rapid prototyping!

See you next time!

--

--

Marcus Cohee

I am starting Unity and learning the ropes as I go! Let’s take this journey together!