New Unity Input System — Scripting Actions

addam davis
5 min readDec 16, 2023

Objective: Create C# scripting for different types of Input!

Now that we know how to create actions in the Action Map, we can now focus on creating code to use these inputs! To begin when you select your Input Asset and, in the Inspector view, you can generate C# Class.

The class name will be named after the Input Asset. Press apply, and you will have your map scripted out automatically.

If you go into your Action Map and make changes these changes will automatically update in the C# script. From this point, create an objects “Player” and give it a script.

Open this new script. To use the new input system, we need to add the Input System library.

Now we have three objectives to get set up the Input System. We need a reference and to start an instance of out input actions. We need to enable input action map. Finally, we need to register perform functions.

Create the reference.

Now we need to create the instance of it in the start method.

This will initialize the instance, once that is done we can move on to enabling the input action Map. We can now enter the input and select what action map we want to enable.

The Player Action Map is now enabled.

With the Action Map enabled we can now register preform functions. If you were to select the input.Player, you can see all the actions we have created.

These are all get values, you can get the shooting action or walk action, these will have events we need to register to. For this tutorial let’s set up the shooting action. Here we are going to determine what happens when we shoot. This is set to the space key, so we need to know when the space key is pressed. When you type out the “input.ActionMap.Action” if you press another . you have some options.

We can know when the action was started, canceled, preformed, etc. When selecting these call backs you will see a lightning bolt in the subtext. This mean that this is an action and Unity will provide the method for us.

the ‘+=’ allows us to add this method to the started event. Anytime we press the space key it will call whatever method we have there. As you can see when the method of shooting started we pass in some context data. The Input Action and Call back context are just parameters that are pass in with the shooting method.

Now we can remove the throw return and place a simple Debug Log to call when this method is being called.

We can also read out what the Call back context is with our debug log. Many people like to rename the obj to “context” which is fine it’s just a variable it doesn’t matter what you call it.

We can now load the editor and test out our shooting method.

And you can see on the consule that it is showing the debug log where the action was taken and all the context which is the action.

We currently do not know when we are done shooting. How do we check for that? we can register another preform function; this one being canceled.

let’s go ahead with another debug log check and along with the context.

You can think of the canceled method as the old GetKeyUp function. When we release the shift key the canceled method will be called.

Now you know how to call for a simple input with the New Unity Input System. Don’t be afraid to experiment with your codes and I’ll see you in the next tutorial!

--

--