Unity’s New Input System: Press and Hold Interaction

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!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Marcus Cohee

Marcus Cohee

20 Followers

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