Unity’s New Input System: Calling Inputs in a C# Script
Hello all!
Today, I will be going over how to make things happen when you press your inputs with code!
Turning your Input Actions Asset to a C# Script
Select your Input Action asset>Look at the Inspector>Select “Generate C# Class”>Hit Apply. That will make a script with the same name and location as your Input Action asset. The script updates whenever you change inputs, so you don’t have to generate every time.
Setting up the Player Script
To make simple movement, setup a vector2 action in your Input Actions asset, and name it walk.
For easy testing, make a cube>name it player>create a new C# script>name it player>assign script to player>open script!
To use the new input system, you need to add the namespace “using UnityEngine.InputSystem;”.
Now, make a variable for the script you generated above, then initialize it in Start. To enable different action maps, you have to enable/disable them. Enable what you start within Start.
To let something happen when you press an input, you have to set it to be performed. By adding _input to a variable this way, it can read the values of Vector2.
When you press an input, it will set that value to 1, meaning it is true. When you make a Vector2 input:
Up: y=1
Down: y=-1
Left: x=-1
Right: x=1
So when I press up, the cube will translate (0, 0, 1)*10*Time.deltaTime, or in other words, forward in a 3D space.
That was how to make a simple movement script using the new input system!
Next up, how to swap action maps!
See you next time!