Unity 2D vector movement with new input system

Daniel Kirwan
4 min readFeb 1, 2022

--

Objective: Be able to move around the screen in 2D with the new input system

From the previous articles where we installed the new input system and then created an action map, now is the time to implement the new system in code to move the player.

For my game I was using the standard axis movement using the keyboard.

The above is the usual way you get the horizontal and vertical movement for 2D motion in Unity. You would then combine these into a Vector3 that would allow you to move the player.

Before we start messing around with the original code. We have to set up the action map in the script and then subscribe to some events so that we can use the actions we have made. Create a reference to the player action asset that you have created. Mine is called PlayerInputActions.

Then in the start method we need to initalise and set up the event subscriptions.

The first line creates a new reference to the action map asset. On line two we make sure the the action map we’re enabling is the one we want. I have three action maps in my asset. Player, ship and Menu.

I am enabling the Player action map, but if I wanted different controls for the main menu I could create the action map and then once the menu has loaded, I would enable the menu action map/controls.

Lines 3 & 4 are me subscribing to the events for the action of performed. There are different events that can be subscribed to for each action, which can be viewed here. We have waiting, started, performed and cancelled.

For my simple movement, I just want to know when the action has been performed.

I will start with the button action for firing in the game. A little tip when subscribing to the events once you have pressed += you can just press tab and it will auto fill in the event and the method.

The new method call is very similar but you can see that I do not need to insert and key presses or manually input which buttons need to be pressed for the player to fire the weapon. I have set up three buttons in the action map linked to Fire action. So pressing any of them will fire a laser.

Next is the movement method. Below you can see the original code used for the movement of the player.

You can see that I’m using the Horizontal and vertical movement system from the old input system.

Above you can see that I am using the input system. I have a simple bool check to make sure that the Player action map is enabled. I then have 1 line which will store the movement information that I need from either the keyboard or a joystick.

If you look closely between the two screenshots you will see that the transform.Translate is slightly different in the new updated version. Instead of having to create a new Vector3 and pass in two vectors I can just use the new move vector.

The line that is most important is below.

This will get the values of the action and store them for us to use in our movement. You can access the x and y values like I do in the lines below the move by using the dot operator.

If you found this helpful, don’t forget to clap and follow for more articles on game development in Unity.

--

--