How to Swap Action Maps
Objective: Learn how to change between two action maps using the new Unity Input System.
If you’re unfamiliar with creating actions please follow this link,and to learn the basics of scripting actions, follow this link. This article will jump straight into switching between two existing actions without context on how to create them.
Table of Contents
· Boarding the Vehicle
· Changing Between Actions
· Why use the Bool Variable in our Code? (optional)
· Disembarking the Vehicle — Switch Back to Previous Action Map
· The Result
Boarding the Vehicle
Create the Action Maps
In this example, we’ll be switching between the Player and Vehicle action maps. Within the Vehicle action map, there is an action called “Board Vehicle”, which is triggered by pressing the ‘T’ key on the keyboard.
Note: You can learn about creating Action Maps here.
Setting up the code for the Actions
Create a new C# script to serve as your Player script (or add the following to an existing one). This is where we’ll define and manage the player’s actions. To use the new Input System, add its namespace at the top of the script.
Next, create a reference to the generated Input class (its name should match the one you just generated — autocomplete will help if enabled). The reference should be a global variable.
Then we initialize the variable in the Start method.
Now, we can enable input by calling the Enable
method. This activates the input system for your objects, allowing you to access them via code.
Press a Key to Board the Vehicle
The Board action is bound to the T key. When T is pressed, we want to switch from the Player action map to the Vehicle action map. To handle this, subscribe to the action’s performed event inside Start
; Unity will then generate the corresponding callback method for you.
To generate the peformed method, you must press TAB.
Changing Between Actions
Inside the performed method, we’ll define the logic for switching between action maps.
Is the Player Driving?
Begin by checking whether the Player is currently inside the Vehicle. This determines which input map should be active. I use a bool
variable to track whether the Player controls are currently enabled—that is, whether the Player is being controlled instead of the Vehicle.
Note: You can skip the
bool
check if you don’t use theUpdate()
method for input handling. I explain why this variable is useful later in the article, or you can jump to that section using the Table of Contents.
By default, the Player is in control, so we set this to true
in the Start
method.
Swap from the Player to the Vehicle’s Action Map
To switch control from the Player to the Vehicle, we’ll update our bool
to false
when the 'T' key is pressed. This logic belongs in the performed
method we generated earlier. However, to keep things clean and organized, we'll move this logic into a dedicated method called EnableVehicle
, and simply call it from within the performed
method.
Code Key Points:
- The bool is set to false to signal that we are no longer controlling the Player
- Disable the Player, and Enable the Vehicle. By disabling the Player Input and Enabling the Vehicle’s we effectively swap between the two action maps.
Now, we simply call this from theperformed
method.
Why use the Bool Variable in our Code?
Using a
bool
to track control state is optional—but helpful if you're handling input logic inside theUpdate
method.
If your movement code relies on Update
(as most input systems do), you’ll need a way to determine which set of logic should be active. In this case, I have two movement functions—each tied to a different action map—and the bool
helps toggle between them as needed.
While we’re still using the Input System here, movement logic isn’t placed directly inside the performed
methods. That’s because actions triggered this way only respond to discrete key presses—meaning the player would have to keep pressing the key repeatedly for movement to occur. For smooth, continuous movement, the Update
method is a better fit. I’ll cover this in a separate article to keep this one focused.
Disembarking the Vehicle — Switch Back to Previous Action Map
I created a new Action Map with the “R” key assigned to the Disembark action.
Re-enable the Player
Now let’s create a method called EnablePlayer
, just like we did earlier with EnableVehicle
.
In the newly generated performed
method for the "R" key, we can simply call EnablePlayer()
.
With this setup, pressing ‘T’ will switch control to the Vehicle, and pressing ‘R’ will return control to the Player.
The Result
As a demonstration: the Player is programmed to rotate, while the Vehicle moves using WASD. I’m swapping between them by pressing the corresponding keys.
I also added a Debug.Log
message inside both EnablePlayer()
and EnableVehicle()
methods so you can see the transitions clearly in the console.
I hope this was helpful :) Check out my portfolio, currently looking for opportunities in the video game industry!