Player Input System

“The player moves with the arrow keys. Ok but how does the game know that?”

Using the Unity Input System Package

Note

For more information on the Input System Package see How to use NEW Input System Package!

Setup:

  1. Select Window > Package Manager

  2. Under the packaged dropdown select Packages: Unity Registry

  3. Click on the Input System Package and click the Install button on the lower right side of the package manager window.

  4. You will get a warning message informing you that the input system package is installed but the backends for the package need to be enabled in the player settings. You can do one of 2 things:

    • Select Yes if you want to enable the new input system and disable the old input system

    or

    • Select No if you want to use both the old input system and the new input system at the same time. After selecting No go to Edit > Project Settings.. > Player > Other Settings. Under the Active Input Handling select Both

How to Receive Player Input

Note

For this example we will process the player pressing the arrow keys and pushing the space bar.

  1. Right click Create > Input Actions

  2. Select the new input actions asset and in the inspector click on the Edit Asset Button.

  3. We click the plus button under the Action Maps column and create a new action map and call it PlayerActionMap

    Note

    Action maps are useful for organizing actions. For example you could have one map for player movement and another map for vehicle movement.

  4. With PlayerActionMap selected, delete all actions under the Actions column. Next click the plus button under the Actions column and create 2 new actions Move and Jump

  5. With the Move action selected we go to the Action Properties column, click on the Action Type dropdown and select Value Since we are looking for a continuous range of inputs for movement (like 0 to 1) instead of a button press that has 2 values (like 0 or 1). Next under the Control Type dropdown we select Vector2 since we will be getting input along 2 axes from the arrow keys. For the Jump action we choose the Button Action type because this action is connected with the space bar.

  6. Under the Move action we click the plus button and select Add Up/Down/Left/Right Composite. We then rename the composite ArrowKeys. For each action below the composite we go under the Binding Properties column, click on the Path dropdown and select the corresponding button we want to have assigned to each binding.

  7. Under the Jump action we click the plus button and select Add Binding. Next under the Binding Properties column, click on the Path dropdown and select the space bar.

  8. Next add an empty game object to your scene and add a Player Input Component to it. Drag your Input Action asset into the Actions parameter and select the Default Map to be the PlayerActionMap.

  9. On the Player Input component, select the dropdown menu called Behavior and choose Invoke Unity Events. Now you will see an Event dropdown appear. Under the Event dropdown expand the PlayerActionMap dropdown and this will give you the ability to trigger effects when receiving input.

Input Binding Remapping

Your game can have the player remap input bindings with the push of a button. This can be accomplished by getting a reference to an input action and running action.PerformInteractiveRebinding(bindingIndex). Input bindings can be extracted from actions through a for loop and you can create a custom editor so that the developer can click on a dropdown to select a specific binding that they want to remap. Below is the script one can use to quickly remap bindings. All that needs to be done is to call the Rebind() method on a OnButtonClick event.

Note

For composite bindings, the binding index starts at 0 at the composite binding and counts up as we move down the bindings below the composite binding. See the image below detailing the binding index for the input actions move and jump:

../_images/binding_index_example.png

Additionally you can quickly reset the input bindings back to the default state with the push of a button. This is accomplished by getting a reference to the input action asset and running map.RemoveAllBindingOverrides() for each InputActionMap in the asset’s action maps.

Example

Important

This example requires the following packages to be installed:

  • TextMeshPro Essentials

  • Input System

Warning

This example also includes the following packages:

So when you are importing, take care to make sure things are not overwritten / duplicated if you have downloaded those packages in the past.

Click to download the input rebinding example InputSystemRebindExample.unitypackage.

Scriptable Object Input Events

In a different section (Event Architecture: Scriptable Objects) we learned how to create an event system from scriptable objects. Now we shall do the same thing except the scriptable object events will be tied to Input Actions.

../_images/scriptable_object_input_system_diagram.svg

Example

Click to download ScriptableObjectInputSystemExample.unitypackage.

Important

This example requires the following packages to be installed:

  • TextMeshPro Essentials

  • Input System

Note

This example also includes utilities for remapping Input bindings as seen in the section Input Binding Remapping

Scriptable Object Input Events V2

We can refactor the above implementation for scriptable objects events so that:

  • The game event listener component we add to game objects can listen in on multiple events.

You can find the updated code and example below:

Example

Click to download ScriptableObjectInputSystemV2Example.unitypackage.

Important

This example requires the following packages to be installed:

  • TextMeshPro Essentials

  • Input System

Warning

This example also includes the following packages:

So when you are importing, take care to make sure things are not overwritten / duplicated if you have downloaded those packages in the past.