How to Swap Action Maps

Alex Tolson
4 min readJun 17, 2024

--

In this article, we’re going to cover how to switch between Actions Maps.

In order to switch between action maps, we need two action maps to switch between:

First, we have our Player Action Map:

which will simply rotate left and right using the A/D/Left/Right arrow keys:

I’ve also included an Invert Processor, this way, the A and left arrow buttons will turn the cube left and the D and right arrow buttons will turn the cube right.

Both the Keyboard and the Arrows key bindings will be a 1D Axis. When the positive or negative is pressed, the value will either be 1 or -1.

We also need another Action that we will use to switch between Action Maps:

Secondly, we have our Driving Action Map:

This is Action type is a Value and its Control type is a Vector 2 which is covered in this previous article. Save your Assets before moving on.

If you are starting from scratch, ensure that you click on the Player Input Actions in the Project view and generate a C# script.

Let us continue by hopping into our Player script, which is placed on our Player object.

In this tutorial, the Player will only rotate left and right. But when the T key is pressed, the Player enters “Driving” mode and can move left, right, forwards, and backwards.

To create these actions, first of all, we need to include our using UnityEngine.InputSystem namespace.

Now, we can get a handle to our Player Input Actions and name that handle _input.

We also want to specify the rotation amount that Player will rotate when the A/D keys are pressed.

Now, inside the Start method, we will complete the reference to the handle and enable our Player Action Map:

Then, we want to set up a delegate for when the Switch Action is performed (as in, if the T key is pressed as that is the key we’ve selected to be our key binding).

So, let’s type out _input.Player.Switch.performed +=

immediately after type “+=” press the Tab key and a method will automatically be created and assigned to the Delegate.

Inside the Switch_performed method, we will disable the Player Action Map and enable the Driving Action Map.

Inside the Update method, we include our logic for rotating the Player when the Player Action Map is active. We have a rotate variable that reads the input of the 1D Axis value (remember it will either be 1 or -1).

Then we use transform.Rotate to multiply the rotationAmount (which is set in the inspector to be 90 degrees around the Y axis), by either 1 or -1, multiplied by Time.deltaTime.

We will also include our logic for when the Driving Action Map is active. We have our move variable that reads the value from the 2D Vector Composite.

We use that information with transform.Translate. We create a new Vector 3 that will take in the x and y values and apply them to x and z axes.

Instead of moving on the x and y, which would give us left, right, up, and down movement, we move on the x and use the y-value for the z-axis. This way, we can move left and right, AND forwards and backwards.

Let’s test our application:

This concludes the tutorial on How to Swap Action Maps.

Thanks for stopping by.

--

--