Made With Unity | 2D Space Shooter Part 9: Unity’s New Input System

In this series of article / tutorials we are going to be making a small 2D Space Shooter using Unity3D using the New Input System.

We are going to be updating the game to use the New Input System from Unity.

Unity
The Input System package implements a system to use any kind of Input Device to control your Unity content. It’s intended to be a more powerful, flexible, and configurable replacement for Unity’s classic Input Manager

With this new system the player can have multiple ways to play our game, without altering our code to much. Players can play using Keyboard and Mouse and using a Gamepad, with minor tweaks the game can be playable on Mobile also.

INSTALLING THE INPUT SYSTEM PACKAGE

Go to the Package Manager, select Unity Registry and type Input System and press Install. It will prompt you to restart, select No.

Got to the Project Settings, on the Player section got to Other Settings tab and scroll down until you see Active Input Handling, select Both and hit Apply.

If we had hit Yes to Restart the first time it ask, there are going to be errors since Unity will stop using the old input system and the code using Input.AxisRaw() will stop working. By selecting Both on the Player setting Unity will allowed us to use both input system without issues.

USING THE NEW INPUT SYSTEM

There are MULTIPLE ways to use the new Input package…..to much that it may need its own articles 😅. We are going to use it in a VERY BASIC way.

ADDING THE PLAYER INPUT COMPONENT

Create a new folder inside your Scripts folder named Input Actions Map. Select your Player game object and add the component Player Input

Press Create Actions and save the following file inside the newly created folder, named the file 2D_Shooter_InputActions. Find the Input Actions and double click it to open it.

By using the Player Input component to create our Input Actions it will give you by default a lot predefine actions for your Player: Move, Look and Fire. We will only be using for now the Move action to move our player in the game and the Fire action to shoot the laser.

ADDING NEW ACTIONS

By default when we use the Fire action it comes predefine to work with the Mouse Left Button and the Right Trigger of a Gamepad/Xbox controller, we are going to add the Space key. First check the box for Auto-Save so anything we do is safe.

On the Fire action, press on the plus sign to Add Binding. Select <No Binding> and go to the Path and collapse it, select Keboard

Press By Location of Key (Using US Layout) and find a select Space

Check mark Keyboard&Mouse

You can also press on Listen and press the key or button from a Gamepad that you desire to Bind to the Action

UPDATING THE PLAYER SCRIPT

Open the Player script, on top of the script file add the using UnityEngine.InputSystem;, every time you want to use the new Input System you need to add this namespace and have downloaded the package.

Inside the Player class we will add the following variables:

On the Start() method we must get the PlayerInput component and initialize the InputAction of _moveAction and _fireAction.

Notice that on _moveAction I'm getting the input that are going to be created when I press any binding inside the Move action. The actions[“Move”] is case sensitive and the Action name must be the same when you saw it on the Input Action Map

If you chance the name of Move to Movement or Fire to Shoot you must do the changes also on the code.

EXAMPLE

_moveAction = _playerInputs.actions[“Movement”]

_fireAction = _playerInputs.actions[“Shoot”]

The reason the component know where to look for the actions[“”] inside the Player Action Map is because on the Player Input component we specified that the Default Map is the Player and not the UI map

MOVING THE PLAYER

Inside your PlayerMovement() method change were your _horizontalInput and _verticalInput are getting the values.

We are using ReadValue<Vector2>() since the Move action is a Control Type of Vector2

Now we can move our player using WASD and the LeftStick of a GamePad

SHOOTING THE LASER

Got to the ShootingLaser() method and do the following changes:

_fireAction.WasPressedThisFrame() dose the same as Input.GetKeyDown(KeyCode.Space), it will accept the action input from the mouse and space bar at soon it is press all the way down. The Fire action is a Action Type of Button.

Save the changes and hit play

Next step is to tackle the UI to make a Score system😆

🎮PART 10 🎮

--

--

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
Rehtse Studio

Rehtse Studio

Game Developer sharing content around Unity, programing and tips and tricks in game dev.