New Input System
Unity's New Input System
Unity's new input system consists of the following components:
- Input Action Assets: A configuration file that contains all the properties of actions and bindings.
- Actions: The inputs.
- Bindings: The relationship between a control on the input device (e.g., key, mouse button) and the action.
- Scripts: Scripts that handle events from an action.
To use the new input system, we first need to install it through the Package Manager.
Receiving Input from an Input Device
To receive input from an input device such as a keyboard or joystick, follow these steps:
1. Assign a `PlayerInput` component to your player object.
2. The component must be linked to input actions to effectively receive events. Click on the "Create Actions" button.
This will create an `.inputactions` file/asset in your project. Once created, you can double-click it to open the file/asset.
3. Assign this file/asset to the `PlayerInput` component you just added to your player.
Moving the Player with the Keyboard
Let's make our player move across a plane using the keyboard.
1. Create a new Action Map named "Test" with the following actions:
- Horizontal: Set as a 1D Axis, with the negative value bound to the left arrow key and the positive value to the right arrow key.
- Forward: Set as a 1D Axis, with the negative value bound to the down arrow key and the positive value to the up arrow key.
The names "Horizontal" and "Forward" are important because you can now reference these events in your script with an `On` prefix, such as `private void OnHorizontal` and `private void OnForward`.
2. Create a new script and attach it as a component to your player, then set the default map to "Test" (the name you just created).
The Movement Script
Here's what the movement script looks like:
using UnityEngine;
public class Movement : MonoBehaviour
{
[SerializeField]
float moveSpeed;
float horizontal = 0;
float forward = 0;
private void FixedUpdate()
{
transform.position += (moveSpeed * horizontal) * Time.deltaTime * Vector3.right;
transform.position += (moveSpeed * forward) * Time.deltaTime * Vector3.forward;
Debug.Log(transform.position.z);
}
private void OnHorizontal(InputValue value)
{
float data = value.Get<float>();
Debug.Log(data);
horizontal = data;
}
private void OnForward(InputValue value)
{
float data = value.Get<float>();
Debug.Log(data);
forward = data;
}
}
In the `OnHorizontal` and `OnForward` events, an `InputValue` is passed, which I convert to a float (since we chose a 1D axis in the action). This value is either +1 or -1, depending on whether the left, right, up, or down arrow key is pressed. In the `FixedUpdate` method, I've coded the movement.
Another Example: 2D Vector Input
There are also other options you can use. Here's another example:
1. I created an Action Map named "Vector" with a `Move` action that represents a 2D vector.
2. The Up, Down, Left, and Right bindings are connected to the keyboard arrow keys.
- (Don't forget to change the default map in the `PlayerInput` component to "Vector".)
This is the Movement script that I linked:
using UnityEngine;
public class MovementVector : MonoBehaviour
{
[SerializeField]
float moveSpeed;
Vector3 direction = Vector3.zero;
// Update is called once per frame
void Update()
{
transform.position += moveSpeed * Time.deltaTime * direction;
}
private void OnMove(InputValue value)
{
Vector2 data = value.Get<Vector2>();
direction = new Vector3(data.x, 0, data.y); // Mapping to forward direction
Debug.Log(data);
}
}
In the `OnMove` method, I convert the 2D vector to a 3D vector and map the up direction to a forward direction to navigate in a 3D space.