Home and Learn: Games Programming Course


The Input System in Unity 6

In this lesson, we'll take a closer look at Unity's Input System. In Unity 6, the process has been simplified. You'll see how easy it is now to get a character moving, and detect things like button presses. Off we go then!

 

Create a new Unity Project, any pipeline will do. In Unity 6, the default is the Universal render Pipeline, which is fine.

One thing you need to check is that Unity is using the correct input system. To check, click Edit from the menus at the top of Unity. From the Edit menu, select Project Settings. Select Player from list on the left. Under Configuration, make sure Input System Package (New) is selected from the dropdown next to Active Input Handling (you can select Both from the dropdown, if you prefer):

The Project Settings dialog box checking if the Input Manager is set correctly

Close the Project Settings screen and have a look at the Project area at the bottom of Unity. In the Assets folder, you should see an item called InputSystem_Actions:

Unity 6 Assets folder in the Project area

The Input System has been added to your project by Unity and is a list of common actions that you'd typically want in a game for a player and a user interface. It's all set up for you and ready to go. (You can add your own actions. Or even set up your own Input System from scratch.)

Double click InputSystem_Actions. It will look like this at the time of writing (Unity 6, June 2025):

The Input Systems Actions dialog box in Unity 6

On the left is where you set up your Action Maps. Two have already been set up by Unity, a Player action map and a UI action map. You can add as many action maps as you want here by clicking the little plus button to the right. But make sure the Player action map is selected on the left.

In the middle is where you set up Actions. You can see there are quite a lot already set up: Move, Look, Attack, etc. An action is just about anything you'd want to do in a game.

Expand the Move action to see the following

The Move action highlighted in the Unity Input System dialog box

The items under Move are all bindings. What you are binding here is an input from things like a Gamepad. You are trying to tell Unity what should happen when you hit a certain key on a keyboard, or press a specified button on a Gamepad - anything to do with a game controller can be bound to an action. We'll add our action and binding soon. For now, take note of the names of each action (Move, Look, Attack, Interact, etc.). The names are very important.

Another thing to take note of are the Action Properties on the right. Click on the Move action to highlight it. Note the Action Properties heading and the Action Type item:

Action Properties settings

If you click the dropdown list to the right of Action Type, you'll see that there are three types to choose from:

Action types in a dropdown for a Unity game: Value, Button, Pass Through

The options are Value, Button, Passthrough. We'll use button in this lesson, as well as value. But here's what they all mean:

Value: This is used for inputs that continuously change, such as an analog stick, a trigger or the WSAD keys on a keyboard. It tracks the input's value over time, making it ideal for movement or aiming mechanics.

Button: This is for discrete inputs, like pressing a key or clicking a mouse button. It triggers an action when the button is pressed and released, making it perfect for things like jump, interact, and attack commands.

Pass Through: This allows raw input data to be passed directly to the action without additional processing. It's useful when you want to handle input manually, such as reading raw sensor data or handling multiple simultaneous inputs.

If you select Value, you'll see a second dropdown list for Control Type. For the Move action, Unity has selected Vector2 for the Control Type. This will be important for us. If you're not sure what a Vector2 is, it's just the X and Y values:

Vector2(X, Y)

If, say, the W key on keyboard is pressed, the Input System would record a value for X as 0 and Y 1. So the Vector2 would be:

Vector2(0, 1)

If, on the other hand, the S key is pressed, the Input System would record a value for X as 0 and Y -1. The Vector2 would then be:

Vector2(0, -1)

 

Let's move on and see how easy it is to put all this into practice as we set up a capsule to act as a player. We'll use the Move action to move the player around.

One last thing to notice before you close down the InputSystem_Actions screen is the Save Asset button, which is top right in Unity 6. Make sure to click it before closing:

The Save Assets button being highlighted in Unity's Input System Actions dialog box

Back in Unity, add a Plane to your Scene. (Right-click in the Hierarchy and select 3D Object > Plane.) You can add a material to your plane to liven it up a little. Make the Plane bigger in the Inspector on the right.

To act as player, right-click in the Hierarchy again. This time, select 3D Object > Capsule. In the Inspector on the right, set the Y Position transform to 1, just to raise your player above the ground.

Make sure your Player is selected in the Hierarchy. In the Inspector on the right, click the Add Component button. Type Player:

A Player Input component being added to a Player game object in Unity 6

From the list, select the Player Input component. You'll to see this added to your player:

The Player Input component in the Inspector with the events highlighted

One crucial thing to note here is all the events that have been set up for you, the ones highlighted by the red box, in the image above. These events are the key to hooking up that Input Actions screen you've just closed down.

You can see that each of those Actions from the middle of the InputSystem_Actions screen has an event. For example, there is a Move action. The corresponding event to go with the action is the OnMove event. Likewise, there is an Attack action set up. If you look closely at the image above, you'll see an event called OnAttack. The same is true for all the actions set up in the InputSystem_Actions assets - for each action there is an event set up in the format On + ACTION_NAME.

Input Actions and there respective events in Unity 6

Now let's write some code to make use of all this. In just a few lines, we can get our Player character to move around in the game.

 

Player Move

Click the Add Component button again. Add a New Script. Call it InputScript. Double click the name of your script to open it up in your code editor. Delete the Start method. Add this:

void OnMove()
{
	Debug.Log("W, A, S, D KEY PRESSED");
}

This is what your script should look like:

A c# script implementing the OnMove method in Unity 6

Surprisingly little code! Save your work and go back to Unity. At the bottom of Unity, make sure the Console is showing. If you can't see a Console, click the Window menu at the top of Unity. From the Window menu, select Panels > Console.

Now play your game. Hit one of the move keys (WASD). You should see the debug message from above logged in the Console. (Make sure to click inside the game window.)

Stop your game and go back to your code. Add a Vector2 variable just above OnMove:

private Vector2 move;

void OnMove()
{
	Debug.Log("W, A, S, D KEY PRESSED");
}

In between the round brackets of OnMove, add an input value variable as a parameter:

void OnMove(InputValue val)

You may get a red underline. If so, add this line at the very top, just below the using statement:

using UnityEngine.InputSystem

Now change OnMove to this:

void OnMove(InputValue val)
{
	move = val.Get();
	Debug.Log("MOVE KEY: " + move);
}

Your code should look like this:

The OnMove method with a value paramater added and a Vector2

Save your code and go back to Unity. Play your game again. Press a move key and watch what happens in the Console. You should see this if you hit the W key:

MOVE KEY: (0.00, 1.00)
MOVE KEY: (0.00, 0.00)

When the W is pressed on the keyboard, the Vector2 is X=0 and Y=1. It resets to 0,0 when the key is not pressed. Here are the values for all the keys:

W: Vector2(0, 1)
S: Vector2(0, -1)
A: Vector2(-1, 0)
D: Vector2(1, 0)

Now let's make use of those values to move the player.

In your Update method, add the following line:

transform.position += 5f * Time.deltaTime * new Vector3(move.x, 0, move.y);

All it does is to move the transform, which is the Player. Notice we're creating a Vector3 for the X, Y, and Z positions of the Player. But the X and Y are coming from our move variable (the Y is zero as we're not jumping up). We got the values for move in our OnMove method.

The whole of your code should look like this:

A C# script to move a player using Unity's new Input System

Save your work and go back to Unity. Play your game. Hit the WASD keys and you should see your Player capsule moving around. Not bad for a few lines of code!

Now let's see how to detect button presses. It's pretty much the same thing.

 

Detect Button Press

In our list of events set up by the Player Input component, we have one called OnInteract. This goes with the Interact action:

An Interact action being mapped to an OnInteract event in Unity 6

Which means we only need to add an OnInteract method to our code.

Go back to your coding window. Add a new boolean variable at the top, just under the Vector2 move variable will do:

bool hasInteracted = false;

(We've called our bool hasInteracted but, just like all variable names, you can call it almost anything you like.)

Now add this method to your code:

void OnInteract()
{
	Debug.Log("INTERACT KEY PRESSED - E");
}

Your code should look like this:

An OnInteract C# method added to get a keyboard key press

Save your code and go back to Unity. Play your game again. Press and hold the E key on your keyboard, which is the key set up as the interact action in the InputSystem_Actions screen. You should see the message displayed in the console, "INTERACT KEY PRESSED - E".

You'd want to do something useful with this, however. So delete the Debug line from OnInteract. Add this in its place:

void OnInteract()
{
	if (!hasInteracted)
	{
		hasInteracted = true;
		Debug.Log("INTERACT KEY PRESSED - DO SOMETHING USEFUL!");
	}
	else
	{
		Debug.Log("STOP INTERACTING");
		hasInteracted = false;
	}
}

Your code should look like this:

A C# if statement added to the code to detect an OnInteract action

Try it out. Save your code. Go back to Unity. Play your game again. Press and hold the E key on your keyboard. You should see the new message appear, INTERACT KEY PRESSED. Press and hold again and you'll see INTERACT KEY RELEASED. You've just created a toggle! This can be used for something like a torch you can switch on and off.

You might be wondering why you have to press and hold the E key. If you take another look at the InputSystem_Actions screen again, you'll see why:

An Interaction added to an Action in the Unity Input System

An Interactions has been added. The property is Hold > Press Point and has been set to a default of half a second (0.5). This means the key press won't activate unless you press and hold for half a second. You can uncheck the Default box and type another number here, anything from 0 to 1. Or click the little minus symbol to get rid of the interaction altogether (the red arrow in the image above). Click the Save Asset button at the top and close the InputSystem_Actions screen.

Now let's add a new action to InputSystem_Actions.

 

Add a New Button Action to Unity's Input System

Double click InputSystem_Actions again in the project area of Unity.

In the middle, just to the right of Actions, click the plus symbol to add a new action. Call it Rotate:

Adding a new action to the Unity input system

Notice that the Action Type is set to Button, which is the default when you create a new action. You can change this to one of the other states, if you want (Value, Button,Pass Through).

Expand the new Rotate item to see that it has <No Binding>, at the moment:

An action set up without a binding

Under Binding Properties on the right, click the Path dropdown. Select the Keyboard item:

Binding a property to a keyboard

Now click the Listen button and then press the letter R on your keyboard. You should see this:

Adding a binding for the letter R on a keyboard

Under the Rotate action, you should see the new binding:

A new binding added to a keyboard key press in the Input System, Unity 6

If you make a mistake, you can just select the binding and then hit the delete key on your keyboard to get rid of it. To add a new one, click the plus symbol to the right of rotate, indicated by the red circle in the image above. You'll see a dropdown list:

Binding options for Input System Actions in Unity 6

 

Select Add Binding and try again. The other options on the menu are:

Add Positive\Negative Binding
Add Binding With One Modifier
Add Binding With Two Modifiers

Here's what they all do:

Add Binding: This is the most basic option. It allows you to assign a single input (such as a key, button, or axis) to an action. For example, you might bind the "Jump" action to the spacebar.

Add Positive/Negative Binding: This is useful for inputs that have two opposing directions, such as movement on an axis. For example, you could bind "Move Forward" to the W key (positive) and "Move Backward" to the S key (negative), allowing for smooth directional input.

Add Binding With One Modifier: This lets you create a binding that requires an additional key or button to be pressed simultaneously. For instance, you could bind "Sprint" to the Shift key but only when W is also pressed.

Add Binding With Two Modifiers: Similar to the previous option, but requires two modifier keys/buttons. For example, you might bind "Special Attack" to the Spacebar, but only when both Shift and Control are held down.

Save your asset and close the InputSystem_Actions screen. Select your Player in the Hierarchy and notice the Player Input area in the Inspector on the right. You should see that Unity has added a new event for you - OnRotate:

A new event added by Unity to the Player Input component

With the event set up, you can use it just like you did for the OnInteract method you set up.

 

And we'll leave it there. Unity has made great strides in version 6 to simplify the input system for you. This tutorial was made in June 2025. No doubt Unity will make it even simpler in future versions of the software.

<--Back to the Unity 3D Course Contents Page

 


Email us: enquiry at homeandlearn.co.uk