Scripting Actions (New Unity Input System)
After defining Action Maps, Actions, and Bindings in the Unity Input System, the next step is connecting those actions to gameplay logic using C# scripts. This process allows player inputs to trigger character behaviors such as movement, abilities, or interactions.
Unity provides several ways to access input actions in code, but one of the most common approaches is through the PlayerInput component, which automatically links actions from the Input Action Asset to functions inside a script.
Preparing the PlayerInput Component
First, attach a PlayerInput component to the player GameObject. Then assign the previously created Input Action Asset (for example DogControls).
Set the Behavior to:
Send MessagesWith this option enabled, Unity automatically calls functions in the script whose names match the input action names.
For example, an action called Bark will trigger a method called:
OnBark()Bark Action Example
The Bark action is mapped to the Space key and triggers a simple behavior when the player presses the button.
using UnityEngine;
using UnityEngine.InputSystem;public class DogController : MonoBehaviour
{
public void OnBark(InputAction.CallbackContext context)
{
if (context.performed)
{
Debug.Log("Dog Barked!");
}
}
}When the Space key is pressed, the input system calls OnBark() and executes the logic inside the method.
Walk Action Example (WASD Movement)
Movement actions typically return a Vector2 value because multiple keys contribute to the final movement direction. The W, A, S, D keys are configured using a 2D Vector Composite Binding.
public void OnWalk(InputAction.CallbackContext context)
{
Vector2 movement = context.ReadValue<Vector2>();
Debug.Log("Movement Direction: " + movement);
}The returned vector represents the direction of movement:
KeyOutputW(0,1)S(0,-1)A(-1,0)D(1,0)
This value can then be used to control character movement.
Run Action Example (Hold Interaction)
Some actions require the player to hold a key for a specific duration. In this example, the player must hold Left Shift for one second to trigger the Run action.
Get JUMAMUROD ARALOV’s stories in your inbox
Join Medium for free to get updates from this writer.
The interaction is configured in the Input Action editor using:
Hold(duration=1)The corresponding script method looks like this:
public void OnRun(InputAction.CallbackContext context)
{
if (context.performed)
{
Debug.Log("Dog is Running!");
}
}The action only triggers after the key has been held for the specified duration.
Die Action Example (Multiple Keys)
The Unity Input System also supports composite bindings that combine multiple keys. In this case, pressing D and F together triggers the Die action.
public void OnDie(InputAction.CallbackContext context)
{
if (context.performed)
{
Debug.Log("Character died!");
}
}This approach can be useful for special abilities, debug commands, or advanced gameplay mechanics that require multiple inputs.
Benefits of the New Input System
The New Unity Input System provides several advantages compared to the legacy input system:
- Supports multiple input devices (keyboard, mouse, gamepad, VR controllers)
- Uses Action-based input architecture
- Enables composite bindings and modifiers
- Allows input rebinding at runtime
- Separates input configuration from gameplay logic
This modular design makes it easier to create scalable and maintainable input systems for complex games.
Conclusion
By scripting input actions, developers can transform player inputs into meaningful gameplay behaviors. Using PlayerInput, Action Maps, and InputAction callbacks, Unity provides a flexible system that connects input devices directly to game logic.
In practice, this means a simple key press can trigger anything from character movement to complex gameplay interactions, making the input system a central component of modern Unity development.








