2D Vector Composite with New Unity Input System

Heathrileyo
3 min readAug 15, 2022

--

Hello and welcome back! Today will be an especially quick and useful tutorial. We already have a single action that we’ve taken from start to finish. It requires a button and is scripted to print to our console. Let’s work on that WASD 2D Vector and look in close at how it was made and how it’s coded!

You’ll see that we used Control Type Vector 2. This will then actually give us a a new choice when you choose to add a binding, “Add Up/Down/Left/Right Composite”. You can assign whatever keys you like, but for this purpose we’ll be using WASD. Let’s jump into the PlayerInput C# script we made in our last post.

Our DogActions action map is still enabled, so we don’t have to do that again, but we do have to enable the Walk action. Then we’ll connect it just like we did with the Bark action.

You’ll see that I made this print something a little different to our console I’ve changed obj to context and referred to it in the script. It will show “Walking + a context from the key that was pressed”. Why is this important? Let’s press play and find out.

Hmmm looks a little messy but let’s choose on of those messages and see what the full message says.

“Walking{ action=DogActions/Walk[/Keyboard/w,/Keyboard/s,/Keyboard/a,/Keyboard/d] phase=Performed time=15.0277358000021 control=Key:/Keyboard/d value=(1.0, 0.0) interaction= }”

Do you see that (1,0) towards the end? There seems to be a different value for each of the different buttons that was pressed: (1,0), (-1,0), (0,1), (0,-1). Can you guess why we ended up with 4 distinct directions? Up and Down. Left and Right. And we can use that information to plug and play later rather than just printing out a huge stringy message.

Let’s take this a step further and do a mental exercise for fun since it was such a short post. Remember how we made the Run action by holding shift for 1 second? If we create performed method for that (remember we don’t want to use started here because it has to be held down and not at just the start of the press) we could set a bool to true, or increase a speed variable to increase the walk speed of our player.

I hope that our new Unity input system is making sense to you! In our next post we’ll be looking at swapping to a different action map at a time of our choosing. See you again soon!

--

--

Heathrileyo

Full time father of two attempts to fill your prescription and become proficient in the world of game dev