Implementing Unity’s New Input Controls

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:

--

--

--

Unity Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

However, Harbaugh’s window of opportunity to build Michigan Football

Deploy a Django app on Heroku using GitHub

Craftmanship in Quality Assurance Engineering

Google coding interview template

Installing CUDA Toolkit 10.0 and cuDNN for Deep learning with Tensorflow-gpu on Ubuntu 18.04+ LTS

Adding Post Processing Profiles in Unity- Game Dev Series 155

Understanding Play features with DI support

Stop wasting your time with boring file manipulation

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mike Brisson

Mike Brisson

Unity Developer

More from Medium

’Twas The Code Before Christmas

Switching Character Control in Unity: Part II, Implementation

An animated gif. The window is split. On the left, a camera looks at a tank as it first moves towards the camera. The tank then stops and the turret begins to rotate left and right. The animation finishes with the turret elevating towards the camera. On the right, the same scene is shown from the perspective of a camera following the tank. After the tank finishes moving, the camera switches to one looking just over the turret and rotates with the turret as it first trains, then elevates.

How to Remove a Large File from Commit History in Git?

Commit with Git!