0
0
Unityframework~10 mins

Input action maps in Unity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Input action maps
Define Input Actions
Create Input Action Map
Add Actions to Map
Enable Action Map
Listen for Input Events
Respond to Input
Disable Action Map when done
Input action maps group input actions. You define actions, add them to a map, enable the map, then listen and respond to inputs.
Execution Sample
Unity
var playerActions = new InputActionMap("Player");
var jump = playerActions.AddAction("Jump", binding: "<Keyboard>/space");
jump.performed += ctx => Debug.Log("Jump pressed");
playerActions.Enable();
This code creates an input action map named 'Player', adds a 'Jump' action bound to the spacebar, listens for the jump action, and enables the map.
Execution Table
StepActionInputActionMap StateEvent SubscriptionOutput
1Create InputActionMap 'Player'Empty map createdNo subscriptionsNo output
2Add 'Jump' action with spacebar bindingMap has 1 action: 'Jump'No subscriptionsNo output
3Subscribe to 'Jump.performed' eventMap unchangedJump action subscribed to eventNo output
4Enable InputActionMapMap enabledSubscriptions activeNo output
5Press spacebarMap enabledJump.performed event triggeredConsole logs 'Jump pressed'
6Disable InputActionMapMap disabledSubscriptions inactiveNo output
💡 InputActionMap disabled, no further input events processed
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6
playerActionsnullEmpty InputActionMapContains 'Jump' actionSameEnabledEnabledDisabled
jumpnullnullInputAction bound to spacebarSubscribed to performed eventSameSameSame
Key Moments - 3 Insights
Why do we need to enable the InputActionMap before it detects input?
The InputActionMap only listens for input when enabled, as shown in step 4 and 5 of the execution_table. Without enabling, input events won't trigger.
What happens if we add an action but don't subscribe to its events?
Adding an action alone doesn't produce output. You must subscribe to events like 'performed' to respond, as shown in step 3 and 5.
Why disable the InputActionMap after use?
Disabling stops listening to inputs and frees resources, preventing unwanted input handling, as shown in step 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of 'playerActions' after step 4?
AEnabled
BEmpty
CDisabled
DNull
💡 Hint
Check the 'InputActionMap State' column at step 4 in the execution_table
At which step does the 'Jump.performed' event subscription happen?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Event Subscription' column in the execution_table
If we never call 'playerActions.Enable()', what happens when pressing spacebar?
AConsole logs 'Jump pressed'
BError thrown
CNo output, input ignored
DInputActionMap disables automatically
💡 Hint
Refer to step 4 and 5 in the execution_table about enabling the map
Concept Snapshot
Input Action Maps group input actions.
Define actions and add to a map.
Subscribe to action events (e.g., performed).
Enable the map to listen for inputs.
Disable when done to stop listening.
Full Transcript
Input action maps in Unity help organize input controls. First, you create an InputActionMap, then add actions like 'Jump' with specific bindings such as the spacebar. You subscribe to events like 'performed' to respond when the input happens. Enabling the map starts listening for inputs, and disabling stops it. This process ensures your game reacts only when you want it to, keeping input handling clean and efficient.