Racing Car Game # Day 6— New Unity Input System
Objective: Control the car using A Keyboard or Gamepad
In the previous article, Racing Car Game # Day 5 — Wheel Smoke Effect, we added wheel smoke to the prototype, in this article, we will migrate to the new Unity Input System so we can easily add keyboard, gamepad controls and be ready when this moves to mobile.
To use the Input System, we need to import it from Package Manager. Go to Window > Package Manager.
Ensure Packages is set to Unity Registry and go to Advanced Project Settings to enable Preview Packages.
Search for Input System and Install it.
You will get a warning that you will be using the new Input system. Click Yes.
If you decide you want to return to the old Input System, simply go to Edit > Project Settings > Player, and in the Other Settings, find Active Input Handling*
Moving to the new Input System
Create a folder in the Scripts folder called InputActions where we will keep our input action scriptable objects.
Right-click on this folder and Create > Input Actions and name this to PlayerInputActions
Double-click on the PlayerInputActions which will open a window.
In the Action Maps column, click the plus button to add a new Action Map and name it Player that is for the Car player in our game.
Our first Action will be a movement action, rename New action to Move and the Action Type is Value, Control Type Vector 2. Remove the No Binding and add a 2D Vector Composite.
Let’s now bind each of the directions of movement to a keyboard key. In the Actions, under 2D Vector, select Up. Go in Binding dropbox and click the Listen button. This will now listen out for any inputs and display what it received. Press the W key for up and select it from the list.
Repeat the same process for the ASD keys.
Please remember to press Save Asset whenever you make any changes. Now that ‘configuration’ has been create, let’s click on the Car and add a Player Input Component. Drag on the PlayerInputActions we just configured. Since we only have one Action Map, the Player, will be the default Map.
Let’s now go into the Locomotion script and replace the code with the new Input System code.
Add the using statement for the Input System library
Create a reference to the component, get the component and check that it does exist on the game object.
In our Update method, instead of checking the Vertical and Horizontal Axis, we read the value from the Move Action. The y represents is equivalent to the vertical axis and the x represents the horizontal axis. In the Move function, put a zero for braking for now. We will change this shortly.
That is all the changes you need in the code. Press Play and you should have the car accelerating and steering.
Let’s try something different for braking and use Unity’s Event system to detect an event when the brake button has been pressed.
In the Editor, with the Car selected, In the Player Input component, change the Behavior to Invoke Unity Events.
Open up the PlayerInputAction, and add a new Action and name it Braking. Action Type is Button and bind it to the Space key.
Now remember to Save Asset and then head to the Locomotion script to add the behaviour for when the button is pressed.
Create a new private variable for braking. Create a new public method that takes a InputAction Callback context. When a button is pressed, it goes throw three phases:
We want to capture the first instant the button is pressed down which is Started and when the button has lifted, we look out for the cancelled phase.
In the Update method, replace the zero with braking variable.
Save the script and we need to let Unity events know to call this method when the Braking button is pressed.
In the Player Input component, back on the Car, from the Hierarchy, drag the Car (which has the Locomotion script on it) and because we made the function public, the Braking method is available to be selected for the Braking event.
Your car should now have all the inputs working with the new Input System. Now mapping the Gamepad, or other inputs, has never been so simple but before doing this, let’s take advantage of the Control Schemes that neatly group these actions. We will create one for the Keyboard and set the current actions to the Keyboard scheme.
In the PlayerInputAction, Add a Control Scheme, name it Gamepad and choose Gamepad.
Now we will bind the Left stick to Move and Left Trigger for Braking.
That is it!!! Save the Asset and you now have a Keyboard and a Gamepad working.
In the next article, I I wanted to see how this looks on a mobile and I’ll share how I made use of the Device Simulator package, added touch controls and a joystick to be able to simulate it working as a mobile game.
If you enjoyed reading this article give me a Clap, also if you would like to see more, “Follow” me, so you may be notified of future releases. You may also send me a message if you need any help or if there is something you think I can improve.