2D Vector Composite w/ New Unity Input System

Alex Tolson
4 min readJun 15, 2024

--

In the last article, we learned how to use delegates to assign behavior to our Input Actions.

Now we are going to do the same thing, but with a 2D Vector Composite. And we’re going to get our player moving, not just debugging it.

Let’s hop into unity and set up our Player Input Actions.

Let’s add our new binding. We’ll select a Value instead of a Button for our Action Type and change our Control Type to Vector2:

We can now set our 4-way direction binding:

Click on Path and then click on Listen. Press the keys that we want our player to press for Up/Down/Left/Right movement. I’ve chosen WASD.

Save the asset:

It’s important to remember to save your work alot and often.

There is an auto-save feature but again as previously mentioned, any changes will cause Unity to recompile… and time is precious.

In the Project view, select the PlayerInputActions and look to the Inspector. We have the option to Generate a C# class for our PlayerInputActions, which is what we WANT to do. Go ahead and click the checkbox and select Apply:

Now, we can set up our project:

We have a cube that we’ve named Player and it has a Player script attached to it.

Open the Player script for editing.

First things first, let’s add our using UnityEngine.InputSystem namespace. From there we can create a handle to our Player Input Actions and name it _input.

I’ve also included a speed variable:

In the Start method, we will complete the reference by setting _input equal to a new PlayerInputActions. From there we can enable the action map we want to use: the Player action map we just created:

Like in the previous article, we are going to assign a method to our delegate.

With a Vector 2 Composite, there are coordinates that we can access by way of ReadValue. Let’s debug.log that information to see what it looks like:

Here is our request to read value from the vector 2 from the x and y.

NOTE: I’ve set the speed in the Inspector, on the Player script to 5:

Let’s see what information the console gives us:

We know that +1 on the y is up and -1 on the y is down. -1 on the x is left and +1 on the x is right. It’s basically our WASD keys.

How can we use this information to move our cube?

Well, let’s create another method named CalculateMovement:

Here we can store the Vector2 information inside a var variable named move (we can also just use a Vector2 data type but var works just as well).

Now, when we use transform.Translate, we just use our newly create move variable multiplied by speed multiplied by Time.deltaTime.

Last but not least, we need to call our CalculateMovement() method in our Update method.

Let’s see our player move:

We can even achieve a 3d aspect using this information if we use the y value where the z value goes, like so:

Let’s take a look:

With the way we’ve programmed our movement we actually don’t even need the delegate. So, let’s delete the Movement_performed method:

And we don’t need the line where we assign the Movement_performed method to the delegate, so that can be removed as well.

That concludes the tutorial on 2D Vector Composite w/ New Unity Input System.

Thanks for Stopping By.

--

--