Implementing Unity’s New Input Controls

Mike Brisson
4 min readAug 6, 2021

--

If you have used input that is more complex than simple horizontal/vertical movement, you have struggled assigning button presses to different peripherals. Trying to figure out which axis the right trigger on the PlayStation vs the Xbox controllers was annoying, to say the least.

The ‘new’ input system allows developers to assign the functionality in the code, then setup a control scheme taking in account what forms of input you want to allow.

The first thing we will need is to add the package to our project which can be found under Window > Package Manager > Input System

With the input system installed, we need to change the Active Input Handling which is found under Project Settings > Player > Active Input Handling. Here we can change it to the new input system strictly or we can use both:

You will need to restart the editor immediately upon changing this setting

Like most things in Unity, there a several ways to implement the new controls. You could setup your Input Actions from scratch, but I find it a lot more convenient to start with some of the more popular controls already assigned. To accomplish this, simply add a Player Input component to your player, then create a new Action from there:

Once you have named your Input Action, you can drag it to the empty Actions slot:

You will also want Unity to generate a C# script for your newly created actions. This can be done by simply clicking on your Actions (mine are named “PlayerControls”) and select the Generate C# Class option and clicking apply:

If you double click your Actions in the hierarchy, you will see the default setup that Unity has provided:

Any changes to this menu need to be saved manually or have the “Auto-Save” feature enabled in the upper right of the menu

There are 3 columns to the Input Actions - Action Maps, Actions, and Properties. All 3 would be blank if you created this from scratch but here you have a Player and UI map, as well as some predefined Actions and properties for each.

We can see that the Player map has 3 Actions predefined…Move, Look and Fire. Under the Move Action, we can see that WASD have been setup as well as some others such as the Left Stick [Gamepad]. It’s time to access these with a new script.

Create a Player script and drag that onto your player object in the scene. Let’s also add a Character Controller component to minimize our player setup. With the player script open, we’ll add the InputSystem namespace and setup our player controls reference:

If we wanted to use the old Fire() method, we would get access to a button/key and check if it was pressed. Here we already have a number of inputs assigned to the Fire() method, so we just need to indicate what happens when Fire() is called. We can define OnFire() and assign it to the Fire() event right after we have assigned our PlayerControls:

Time to add some movement. We can access our Move Action similar to the Fire Action with a few added steps:

  • First we need to declare a global Vector3 to keep track of our movement
  • Assign our Vector3 in the OnMove method
  • Use our Character Controller Move() method in Fixed Update()

The above code will work until you release your movement button/key. You’ll notice that your character will keep moving until you change direction! That’s because we are only assigning our Vector3 when the button/key is pressed. We also need to check if the button was “canceled”. We can do that in our Awake method too:

Now we are checking when the button is pressed and released:

--

--

No responses yet