Moving with Unity’s New Input System
If you haven’t already got the new Input System running in your project, check out my previous article on how to get it all set up.
Now that we’re all on the same page, let’s get some player movement happening!
Start by creating a primitive shape like a cube or a sphere. This will be our Player. Create a new C# script and attach it to the object — we’ll need this later.
Creating the Input Actions
Look at the Input Actions window (double-click the GameControl you created in the previous article if it’s not already open). In the Action Maps column, click the + to add a new map. Rename the map to Player.
You’ll see in the Actions column that a default action has been automatically created. Rename this action to Move and click the arrow next to it to display its bindings. This should currently say <No Binding>. We actually want to delete this one, so right-click it and select Delete.
Selecting our Move action gives us some information in the Properties column. Change the Action Type from Button to Value. This allows us to set a Control Type, which we’ll make Vector 2.
Now go back to the Actions column, click the + beside the Move action, and select Add 2D Vector Composite. This will allow us to set bindings for the four cardinal directions. Click on each of these, and in the Properties column select the Path drop-down. Set the values as follows:
- Up: Up Arrow [Keyboard]
- Down: Down Arrow [Keyboard]
- Left: Left Arrow [Keyboard]
- Right: Right Arrow [Keyboard]
Note that if you start typing the values, it will find them without your needing to dig through many levels of menu.
Now click Save Asset! Otherwise nothing will work.
Let’s write some code
Open your Player script and add the following line at the top:
using UnityEngine.InputSystem;
Inside the Player class, add the following variables:
private float _moveSpeed = 10f;
private Vector3 _moveVec = Vector3.zero;
_moveSpeed
is the way you can control how fast the player moves. _moveVec
is the direction the player is moving in and so defaults to zero so that the player is stationery if no input is being received.
In Update(), add the following code:
transform.Translate(_moveVec * _moveSpeed * Time.deltaTime);
This moves the player in the desired direction (_moveVec
) at the desired speed (_moveSpeed
) and ties the movement to realtime (Time.deltaTime
) so that the player doesn’t fly off into the wide black yonder never to be seen again.
Underneath the Update() loop, add the following code:
public void OnMove(InputValue input)
{
Vector2 inputVec = input.Get<Vector2>();
_moveVec = new Vector3(inputVec.x, inputVec.y, 0);
}
You’ve associated this method with the Move action by using this pattern to name it: public void On[Action Name Goes Here]()
. For example, a Jump action invokes OnJump()
, while an Attack action invokes OnAttack()
.
InputValue
is a new type you may not know. This class has a Get()
method that you can use to access its values. In this instance, you want the 2D Vector Composite you set in the binding to calculate the movement vector.
When a player presses the Up, Down, Left or Right keys, it passes a Move action to this method, along with the values. Here’s how the key presses affect the values:
- Up: (0, 1)
- Down: (0, -1)
- Left: (-1, 0)
- Right: (1, 0)
- No Key: (0, 0)
- Up and Left: (-1, 1)
Tying it all together
Select the Player object in the hierarchy and add a PlayerInput component. Drag your GameControl onto the component’s Actions field. Make sure the Default Map is set to Player.
Now run the game in Unity. You should be able to use the arrow keys to move the player up, down, left, and right.
What about adding other inputs?
Remember we talked about how easy it is to add other input options? Let’s take a look at that in action.
Suppose you wanted to allow the player to move with the WASD keys as well as the arrow keys. Start by adding a second 2D Vector Composite to the Move action and set the four directions as follows:
- Up: W [Keyboard]
- Down: S [Keyboard]
- Left: A [Keyboard]
- Right: D [Keyboard]
Now click Save Asset and run your game again. Without changing any code, you can now move with either the WASD keys or the arrow keys!