fbpx

Unity Input System

Read Time:4 Minute, 32 Second

As you may know, there are multiple ways to execute a certain task. This is no different when talking about programming. In a previous guide, we explored Player Movement in Unity, and we did it using Unity’s Input API. This time, we will use the Unity Input System. This will simplify input management and will make it easier.

Here is a link to the new system’s official documentation.

Adding the Input System to your project

For us to use Unity’s Input System, we will first need to add its package to our project.
To do that, we will head over to the Package Manager under the Window menu.

Then, we need to look for the package in Unity Registry

Now search for a package called “Input System” and install it.

When you get the following warning, click “Yes”. This warning tells you the UnityEngine.Input APIs will be disabled if you are enabling the new Input System, and it will also restart Unity so be sure to save before that, if you made any changes.

Nice, now we have the Unity Input System package in our project and we can start using it.

Setting up the Input System

To start using the new input system, we need to create a new object called “Input Actions”. Right-click in the project view, go to “create” and then choose “Input Actions” at the bottom, and name it “controls”.

Setting up controls in the new Input System

Now we can start mapping the keys and the events they will trigger once pressed. To do that, double click the “control” Input Actions object we have just created and a new window will open. This window will allow us to manage all the mappings and the events.

To create a new action map, click on the little plus sign on the Action Maps section and name it “Gameplay”. Then, in the Actions section, rename the new action to “Move”, change its Action Type to Value and set the Control Type to Vector 2.

This tells Unity to take the input we are about to map and generate a Vector2 object out of it. We will use this to move the player game object around shortly.
After we did this, let’s set the keys that Unity will use to set the Vector2 values. Remove the “<No Binding>” by clicking it and hitting delete. Then, click the plus sign next to Move and choose “Add Up\Down\Left\Right Composite”, and set its name to “WASD”.

Now, for each direction, set the corresponding keys. That is W for up, S for down, A for left, and D for right by clicking on the direction under WASD, and in the Binding Properties section, under Binding, set Path to the corresponding key.

If you want to also support joysticks, you can do that by adding another Binding and setting its Path to Gamepad -> Left Stick.

Save the Controls asset by clicking the Save Asset at the top of its window, and then close it.

Player Input Manager & Player Input

Now that we have our control object set up, we will set everything we need in the editor. We will start by creating our player game object. For this tutorial, we will use a simple cube and rename it to “Player” but feel free to use what suits you.
After creating the player game object, add a Player Input component to it. Set the Control asset we created in the previous section of the guide as the Player Input component’s Actionamd change the Behaviour to “Invoke Unity Events”.

Adding PlayerController Script and Connecting Everything

All that’s left for us to do is, as the sub-heading says, to add the a PlayerController script that will take care of moving the player for us and to connect everything up.

Attach a new component to your player game object called “PlayerController”. paste the following code in it.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerController : MonoBehaviour
{

    public float speed = 5f;
    private Vector2 movementInput;

    // Update is called once per frame
    void Update()
    {
        transform.Translate(new Vector3(movementInput.x, 0, movementInput.y) * speed * Time.deltaTime);
    }

    public void OnMove(InputAction.CallbackContext ctx)
    {
        movementInput = ctx.ReadValue<Vector2>();
    }

}

The method called OnMove is what we will use to read the user’s input. Then, in the Update method, we will move the player game object accordingly. In Unity, select your player game object, in the Player Input component, expand Events, and then Gameplay. Click the little plus sign under Move, add the player game object and select the method “OnMove” to be called when the Move event is triggered.

Hit play and you can the player moves!

Bonus: Local Multiplayer – Join Player

If you are working on a local multiplayer game, you might want to allow users to join on the go. To do that, make your player game object a prefab. You can do that by dragging the player game object from the Hierarchy window to the Assets window. Then, delete the player game object from the hierarchy window. Select the PlayerManager object and under Joining, set the Player Prefab to the prefab you just created from the player game object.

Hit play and click any key to join. You can connect a controller and press any button to spawn another player and each player will only move according to his input!

Leave a Reply

Your email address will not be published. Required fields are marked *