Charge Jump w/ New Unity Input System
Objective: Learn how to use the Tap and Hold Interactions in the new Unity Input System.
Table of Contents
· Tap
· Hold
· Coding a Charge Jump
∘ Events
· Jump Logic
∘ Cancel Method
· The Result
Tap
A Tap Interaction requires the user to press and release a control within a specific duration to trigger an action. A common example is a jump button — pressing it quickly causes the character to jump.
One useful variation of Tap is Multi-Tap. This interaction allows you to specify a required number of quick, successive taps to trigger an action — such as a double-tap to perform a double jump. The default tap count is 2, but it can be adjusted as needed.
Hold
Hold Interactions are used when an input must be held down for a set duration before the action is triggered. This is commonly used for actions like a charged jump or a charged attack, where holding the button builds up power before releasing it.
In the Binding → Binding Properties → Interactions → + Button → Hold
Change the hold time to one second or as many as you like and now the Player will have to hold down the space key for 1 second before the Sprint Action is registered.
Now that we have an overview on the concepts, let’s take a look a practical example.
Note: In this article, we’ll be jumping straight into the jump logic particular to the Tap and Hold, if you need help setting up a code for the new Input System, check this out.
Coding a Charge Jump
Events
We begin by creating a class reference to our Input Actions and enabling them.
Next, we generate two events in the Start()
method
- One to detect when the jump action is performed (e.g., when the spacebar is pressed)
- Another to detect when the jump action is canceled (e.g., when the spacebar is released)
These event methods are automatically generated using Unity’s event callback system. Simply press the Tab key followed by Enter to auto-complete the function signatures.
Here we have the generated events:
Code Highlights:
- The default parameter is named
obj
, but it's common practice to rename it tocontext
since the data it carries varies depending on the input's context. - To verify if the duration is being properly measured, we use
Debug.Log
. By accessingcontext.duration
, we print the value to the console. This is a simple and effective way to confirm that the input is being registered as expected.
Jump Logic
We’ll use physics-based movement for our jump, so make sure a Rigidbody component is attached to your object.
Cancel Method
First, create a variable for the jump force to be applied to the object.
Here, forceEffect
represents how long the jump key was held. By multiplying the base jump force by this duration, we scale the jump height based on the hold time:
- A short press = small jump
- A long press = higher jump (depending on duration)
Next, we retrieve and store the Rigidbody component:
As a best practice, always check that the Rigidbody isn’t null
before applying physics logic. This helps prevent runtime errors.
AddForce Breakdown:
Vector3.up
applies the force in the upward direction.force
is a multiplier to adjust overall jump strength (this can be a separate configurable variable).(float)forceEffect
ensures that the duration is treated as a float value.ForceMode.Impulse
specifies the type of force, which is important to define clearly—other types includeForce
,VelocityChange
, andAcceleration.
The Result
I hope this was helpful :) Check out my portfolio, currently looking for opportunities in the video game industry!