Upgrading Unity Project From Old Input System to New Part 01 — Player Movement
Objective: To take a legacy project in Unity that uses the Old Input System and upgrade it to the New Input System.
This is the first part of a small series that I am dedicating to taking an old legacy project that uses the Old Input System and upgrading it to the New Input System. To start with I will be looking at upgrading the Player’s movements.
New Input System
First things first, we always need to download and install the New Input System from:
Window → Package Manager → Packages: Unity Registry → Input System → Install.
Then select ‘Yes’ to enabling the backends, which will disable the Old Input System and restart the Editor with the New Input System selected.
Creating an Input Action Asset, Action Map and Actions for Player Movement
Within the Project folder, right-click → Create → Input Actions. I named mine ‘PlayerInputActions’. Open this up and create the Action Map.
On the left hand column under ‘Action Maps’, click the ‘+’ symbol and name this ‘Player’. Now let’s create the Actions for Player movement.
In the middle column under ‘Actions’ click the ‘+’ symbol and name it ‘Movement’. This is going to be an Action Type of ‘Value’ and a Control Type of ‘Vector2’ as we want to continually detect changes and move the player on a X, Z-axis (left, right, forwards and backwards).
Next we need to set Bindings for the ‘Movement’ Action — Click the ‘+’ symbol next to ‘Movement’ and select ‘Add 2D Vector Composite’. Now we need to assign the keys to W, A, S and D respectively. The quickest way is to use Path → Listen → Press the key on the keyboard.
Getting Setup With Coding
As we have built the Action Map and Actions associated for the Player, we now need to be able to access this information through code so we can make our player move.
First, let’s generate a C# class for the Input Action asset we created so we can access the Actions/Bindings:
Next, let’s get a reference to the ‘PlayerInputActions’ asset that we created by opening the ‘Player’ script and initialise an instance of it.
Then we need to Enable() the Action Map and while we are here we can also Disable() for good practice — To prevent those pesky runtime errors popping up when it is trying to find an object it can’t locate.
Normally from here I would then register the Actions to ‘Started’, ‘Performed’ or ‘Canceled’ depending on which events I need (and thus using the Callback system), but this time I am going to directly ‘Poll’ a value from an Action and use this to move the player (article link below).
Updating the Movement Code
Lastly, we need to change a few lines of code in our CalculateMovement() method on the ‘Player’ script.
Old Input System Code:
New Input System Code:
Instead of using the Callback system, I have used ‘Polling’ to directly poll the value of the ‘Movement’ Action when using ReadValue<Vector2>.
Old Input System Movement
New Input System Movement
New Input System — Installation Guide
New Input System — Quick Start Guide
“Upgrading Unity Project From Old Input System to New Part 02 — Interactables”