
Create Input Actions for player character movement
Tutorial
·
Beginner
·
+0XP
·
40 mins
·
Unity Technologies
In this tutorial you’ll use Input Actions, a feature of Unity’s Input System, to implement a more scalable approach to player input for your 2D adventure game.
By the end of this tutorial, you’ll be able to do the following:
- Define and bind Input Actions for your game.
- Read Input Action values.
- Move the player character based on Input Action values.
1. Overview
Player input is a critical part of most games, whatever the players actually control. In 2D Adventure Game: Robot Repair, the player will control character movement around the full environment that you create using an input device. Unity has a package that can help you do this: the Input System.
Note: For the purposes of this tutorial, we chose to use the Ruby’s Adventure asset set, and the images used in instructions will reflect this. If you chose another asset set, the file names will be the same, but in your corresponding theme folder.
2. The Input System package and your game
You’ll use Unity’s Input System package to control the movement of the player character. The Input System is a flexible system that you can use to allow players to control content in Unity with any kind of input device.
For this game, you’re going to focus on a keyboard as the input device, but as you become more experienced you’ll be able to create more flexible input configurations and allow players to remap the controls that they use to play your game.
How does the Input System package work?
The Input System enables you to define particular Input Actions and bind them to controls on an input device. An Input Action is something that the player can do in the game; running, jumping, or interacting with an object, for example. When the player presses a key or button that’s bound to a particular Input Action in the correct way, that action will occur in the game.
For example, you might decide that the player character should jump whenever the player interacts with a certain key on a keyboard. To do this, you need to create an Input Action for jumping, and then bind it to your chosen control.
The scope of this course
We’ve decided to guide you through a very basic approach to working with player input for 2D Beginner: Robot Repair There are additional tools in the Input System that you can use to work with inputs in more efficient ways, including Input Action assets and the Player Input component. However, the workflows for these tools require more scripting than the beginner approach that we’ve chosen for this course.
If you’re a more experienced developer, you can find more information about these tools and workflows in the Input System package documentation.
3. Define an Input Action
Instead of checking for input from a particular device, you can define an Input Action and assign (bind) different input methods to it. This means that you only need to check the Input Action itself, and the package will handle the recognition of the input from the device.
To define an Input Action for left movement, follow these instructions:
1. Open your PlayerController script in your IDE, if you haven’t already done so.
2. At the top of the script, beneath the existing namespace and before the class declaration, add the following line of code:
using UnityEngine.InputSystem;This instruction tells the computer that the script needs to use the Input System package’s features and functionality.
3. Add the following line of code to the beginning of the script class, after the first brace, and before the Start method:
public InputAction LeftAction;This instruction defines an Input Action called LeftAction. The keyword public means that this InputAction will be available for you to edit directly in the Unity Editor.
4. Save your script changes (Ctrl/Cmd+S) and return to Unity Editor.
5. In the Hierarchy window, select the PlayerCharacter GameObject, then locate the Player Controller (Script) component in the Inspector window.

6. Select the Action Properties button (gear icon).
7. Set the Action Type property to Button.

In this game, the player character will only move based on a button press, so Button is the right type for this Input Action.
4. Add a binding for the Input Action
As mentioned earlier, a binding refers to the act of connecting an Input Action to a specific key or control. To add a binding for the Input Action you’ve created, follow these instructions:
1. In the Hierarchy window, select the PlayerCharacter GameObject.
2. In the Player Controller (Script) component, go to Left Action in the list and select Add (+) > Add Binding. This creates an empty binding.
3. Double-click the empty binding in the list to open the Binding Definition window.
4. Open the Path property dropdown and select Listen.
The Path property determines the path to specific input that you bind to the Input Action. The Input System will list all the input binding options that correspond to the input you provide when you do this.
5. Press the Left arrow key on your keyboard and then select Left Arrow [Keyboard] in the Binding Definition window.
Note: You can also navigate through potential inputs in the Binding Definition window, but listening is a more efficient way to create the binding in this situation.
6. The binding path that you’ve selected will now be displayed in the Left Action list in the Player Controller (Script) component.
5. Configure a Value Input Action
You could continue to configure the player character movement by adding individual Input Actions for each direction, but there are limitations to this approach:
- Configuring individual Input Actions isn’t a very efficient way to work at scale — it would be a lot of effort to bind multiple input devices for each Action.
- The current approach to movement is binary input (button pressed or not pressed), but for player movement, you might want more granular input. For example, you might want to use a joystick so the character walks slowly if it is barely pressed or runs when the controller is pushed all the way in a particular direction.
A Value type Input Action addresses these issues by returning a value instead of just checking whether a button is pressed or not. A Trigger input device would provide a float value (based on how hard the trigger is pressed), and a Joystick would provide a Vector2 value indicating direction of movement on horizontal and vertical axes. In this case, a Vector2 Value Move Action will handle the input that you need for player movement in your adventure game.To create and configure a new Value type Input Action, follow these instructions:
1. In the PlayerController script, at the top of the script class, rename the LefAction Input Action to MoveAction:
public InputAction MoveAction;2. Save your changes (Ctrl/Cmd+S) and return to the Unity Editor.
3. In the Hierarchy window select the PlayerCharacter, and in the Inspector window, under the Player Controller (Script) component, locate the Move Action value.
4. Select the Action Properties (gear icon) button, change the Control Type property to Vector2, and ensure the Action Type property is set to Value.
5. Select the Add (+) button, then select Add Up/Down/Left/Right Composite. You can use composite bindings to bind multiple keys to a direction. This approach means that the player can move the character diagonally, if they want to.
Note: You can keep the default name 2D Vector or use the context menu (right-click) to rename it.
6. Assign the relevant keyboard keys to each direction sub-binding, just as you did earlier for the Left Action binding.
Note: You can choose any keys you prefer, but the most common options are the arrow keys (Up, Down, Left, Right) or the WASD keys.
7. Save your changes in the Editor.

6. Revise the Player Controller script
To adjust your PlayerController script to use the new Input Action, follow these instructions:
1. To enable the MoveAction, add the following line of code inside the Start function.
MoveAction.Enable();2. Inside the Update function, replace all the existing code with the following lines of code:
void Update()
{
Vector2 move = MoveAction.ReadValue<Vector2>();
Debug.Log(move);
Vector2 position = (Vector2)transform.position + move * 0.01f;
transform.position = position;
}What do you think that this updated code does? Take a moment to consider before reading on!
Here’s an explanation of this code:
- The first instruction uses ReadValue to get the current value of the MoveAction function, rather than determining whether or not a key is being pressed, as the previous code did.
- The value type that is read is contained within angle brackets (<>) — in this case, it’s a Vector2 value because the input controls movement on two axes.
- The PlayerCharacter GameObject’s position will now be updated based on its previous position, the direction indicated by the pressed key, and a movement increment value of 0.01f.
3. Save your changes and return to the Unity Editor.
4. Enter Play mode to test your changes.
The PlayerCharacter GameObject will now move in all four directions using the MoveAction function.
7. More things to try
If you want to further develop your skills, explore new concepts, or improve your project, check out some of the optional activities below. Each one is tagged as either Easy, Medium, or Difficult, so you can choose the level of challenge.
These activities are entirely optional, so if you’re not interested, no problem — just skip this step. We do recommend attempting at least one of them in order to get the most out of this learning experience. Good luck!
Easy: Add an additional composite binding
Try adding an additional composite binding for the MoveAction property so that your players can use alternative keys (such as the WASD keys) to move the player character.
Medium: Add a binding for gamepad input
If you want to challenge yourself and you have a gamepad available, you can try to add an additional binding for gamepad thumbstick input.
8. Check your script
Take a moment to check that your script is correct before continuing.
PlayerController.cs
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
public InputAction MoveAction;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
MoveAction.Enable();
}
// Update is called once per frame
void Update()
{
Vector2 move = MoveAction.ReadValue<Vector2>();
Debug.Log(move);
Vector2 position = (Vector2)transform.position + move * 0.01f;
transform.position = position;
}
}
9. Next steps
In this tutorial, you implemented the player character movement that you’ll use as the foundation for the rest of your game, using an approach that has the potential to support more input devices in the future.
You’ll finish this unit of the 2D Adventure: Robot Repair course by making your game frame rate independent, which will improve the player experience of movement.