Consider this Unity C# code snippet using the New Input System:
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
public InputAction jumpAction;
void OnEnable()
{
jumpAction.Enable();
jumpAction.performed += ctx => Debug.Log($"Jump performed at {ctx.time}");
}
void OnDisable()
{
jumpAction.Disable();
}
}If the player presses the jump button at game time 5.2 seconds, what will be printed?
using UnityEngine; using UnityEngine.InputSystem; public class PlayerController : MonoBehaviour { public InputAction jumpAction; void OnEnable() { jumpAction.Enable(); jumpAction.performed += ctx => Debug.Log($"Jump performed at {ctx.time}"); } void OnDisable() { jumpAction.Disable(); } }
Look at the ctx.time property in the callback. It gives the time of the input event.
The performed callback runs when the jump button is pressed. The ctx.time holds the game time when the input happened, here 5.2 seconds, so the output is exactly "Jump performed at 5.2".
In Unity's New Input System, where are the player's input bindings (like keyboard keys or gamepad buttons) stored?
Think about the asset that holds all actions and their bindings.
The InputActionAsset is the main container that stores all input actions and their bindings. It can contain multiple InputActionMaps, but the bindings are defined in the asset.
Given this code snippet:
var moveAction = new InputAction(type: InputActionType.Value, binding: "/leftStick"); moveAction.Enable(); var value = moveAction.ReadValue<Vector2>(); Debug.Log(value);
If the left stick is centered (not moved), what will be printed?
var moveAction = new InputAction(type: InputActionType.Value, binding: "<Gamepad>/leftStick"); moveAction.Enable(); var value = moveAction.ReadValue<Vector2>(); Debug.Log(value);
Think about the default position of a gamepad's left stick when untouched.
The left stick centered means no input, so the vector value is (0,0). The ReadValue<Vector2>() returns this neutral position.
Look at this code:
public InputAction fireAction;
void Start()
{
fireAction.performed += ctx => Debug.Log("Fire!");
}
void Update()
{
if (fireAction.triggered)
Debug.Log("Triggered");
}Why does pressing the fire button never print "Fire!"?
public InputAction fireAction;
void Start()
{
fireAction.performed += ctx => Debug.Log("Fire!");
}
void Update()
{
if (fireAction.triggered)
Debug.Log("Triggered");
}Check if the InputAction is enabled before use.
InputActions must be enabled to listen for input and trigger callbacks. Since fireAction.Enable() is missing, the performed event never fires.
In Unity's New Input System, what feature lets you manage multiple players each with their own input devices, so they don't interfere with each other?
Think about the system that tracks users and their devices.
InputUser is the feature designed to handle multiple players by associating input devices with users, allowing separate input contexts for each player.