2D Vector Composite with New Unity Input System

Simon Pham
2 min readJan 16, 2024

--

What is a 2D Vector Composite?

In the context of game development, a 2D Vector Composite represents a 4-way button setup where each button corresponds to a cardinal direction. This is useful for representing up-down-left-right controls, such as WASD keyboard input.

How do we configure the movement with the New Unity Input System?

Firstly, we need to create an Action Map for our character. Additionally, I will create an Action called “Movement,” which is of “Value” action type and “Vector 2” control type.

Player Action Map

Next, I’ll generate a script from the Action Map named “PlayerInputActions” and create a new script called “Player.” I’ll then attach it to the Player object, which is a simple cube.

After that, let’s write a script to actually make the cube move. In the previous blog post, we discussed a 3-step process for interacting with the New Input System. Now, let’s apply that to this movement configuration.

Step 1: Get a reference to and initialize Input Actions

private PlayerInputActions _input;
private void Start()
{
_input = new PlayerInputActions();
}

Step 2: Enable Input Action Map

 _input.Player.Enable();

Step 3: Register perform functions

For this scenario, we’ll approach step 3 a bit differently. Instead of waiting for the action to be started, performed, or cancelled, we’re going to directly read the value from the Movement action:

private void Update()
{
var move = _input.Player.Movement.ReadValue<Vector2>();
}

And to move the character, let’s just simply use the Translate method:

private float _speed = 5f;

private void Update()
{
var move = _input.Player.Movement.ReadValue<Vector2>();
transform.Translate(new Vector3(move.x, 0, move.y) * Time.deltaTime * _speed);
}

Here’s the full code:

using UnityEngine;
using UnityEngine.InputSystem;

public class Player : MonoBehaviour
{
private PlayerInputActions _input;
private float _speed = 5f;

private void Start()
{
_input = new PlayerInputActions();
_input.Player.Enable();
}


private void Update()
{
var move = _input.Player.Movement.ReadValue<Vector2>();
transform.Translate(new Vector3(move.x, 0, move.y) * Time.deltaTime * _speed);
}
}

Now, our character can move along the x and z axes:

--

--