Unity new Input System : Useful Tips (Part VII)

Matteo Lo Piccolo
4 min readDec 31, 2021

--

In this section we see some useful tips.
For example, we can implement logic to go from one actual input to another. As we have seen in previous articles, with this system we can have a different input system for everythings in our game: Player, Vehicles, maybe some turrets in some parts of the game.
So we can have three different Input for three differne objects.
The question is : how can we switch from one system to another?

Let’s try to understand in code.

I recreate the Input, and now I have Player and the Car (for example)
and for now both are empty.

To make things a little easier, I want to switch between player and car, but only the car can move with the input.
In Player we can only press a button to switch to auto input.

First of all, in Player we need to set an Action to switch the input.
And we want to do this by press the T button.

So we create a “DriverState” or “Drive” or “ChangeInput”, the name is not so important and ad it to the Player

We need ActionType button, and we set T as keyboard button

Now, by following the logic, we need a Movement Action in Car, so we can move right?

This time we need a value, not a button, and pass a Vector2 as parameter.

Cancel the actual binding, we need 2D Vector Composite

I personally using WASD

Now, we have the structure of logic :
when we are in Player Input, if we press T we enable the Car Input System.
Let’s go to the code and set it up!

As always, we take the reference and enable the Input

Now, we need to access to
_input -> Player -> DriverState -> performed
and create a DriverState_performed

Now, inside here, we have to enable the Car Input right?
So we can write

Now that we have enable the Car Input System, let’s set up the Car Movement Action.
Exactly how we did for the previous article, we need to set up the movement in Update.

If we now press Play

It works perfectly.

IMPORTANT NOTE : Now, we need to pay attention to this : I use this type of method.
And in the following articles I stick with this for now, BUT this is obviously is not the only method which can you use it for switching Input.
In fact the new Input System has a TONS of feauture, and is nearly impossible touch everything in few articles.
This is really the basics to try ti use it, and as you can see, it works without any problems, but for your info, there is a component called Player Input, and after you added at your (presumably) Player, it give you access at more feauture.
And it is base on Unity Events, so directly in that component you can manage your events.
And obviously, if you get that component in code, it give you more function to use.
One of them, is SwitchCurrentActionMap(String)
Here is a reference
https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/api/UnityEngine.InputSystem.PlayerInput.html#UnityEngine_InputSystem_PlayerInput_SwitchCurrentActionMap_System_String_

To start to use it in any case it’s perfectly fine use
Create -> Input Actions
And we manage in code what we need to do, but for better knowledge, always prioritize at DOCUMENTATION.
There is always what we need to know to test or try things.

Before finish, another useful info :
Here

We enable the Car Movement.
But generally, when we use Player, we can move , so inside here we disable the Player and enable the Car

And in Update we can create the logic based on what Input we use, like this

I hope this can be helpful, we see us in the next articles!

--

--

Matteo Lo Piccolo

Always in love with programming, even if late (I'm already 39 years old) I decided to follow my dream! We will see how far my passion will take me!