Consider this Unity C# code snippet using the new Input System. What will be printed when the button is pressed?
using UnityEngine; using UnityEngine.InputSystem; public class TestInput : MonoBehaviour { public InputAction action; void OnEnable() { action.Enable(); action.performed += ctx => Debug.Log("Button pressed"); } void OnDisable() { action.Disable(); } }
Think about when the performed event triggers and what enabling the action does.
The performed callback runs only when the input action is triggered (button pressed). The action must be enabled to listen for input. So nothing prints until the button is pressed while enabled.
In Unity's Input System, which InputAction type should you use to get smooth, continuous movement input like from a joystick or WASD keys?
Continuous movement needs a value that changes over time, not just a press or release.
The Value type action returns a continuous value (like a Vector2) representing joystick or keyboard axis input, perfect for smooth movement.
Given this code, why does the performed callback never run?
public class PlayerInput : MonoBehaviour { public InputAction jumpAction; void Start() { jumpAction.performed += ctx => Debug.Log("Jump!"); } void OnEnable() { // jumpAction.Enable(); } void OnDisable() { jumpAction.Disable(); } }
Check if the action is enabled anywhere.
The action must be enabled to listen for input. Since jumpAction.Enable() is commented out, the action never listens and the callback never runs.
Which option contains the correct syntax to subscribe to an InputAction's performed event?
InputAction action = new InputAction();
Remember how to add event handlers in C# with lambda or delegate syntax.
Option B uses the correct lambda syntax with explicit parameter type and braces. Option B tries to assign instead of add. Option B uses delegate without parameters, which is invalid for this event.
You have an InputAction named moveAction set to type Value with a Vector2 binding. How do you read its value inside Update() to move a character?
public class PlayerController : MonoBehaviour { public InputAction moveAction; public float speed = 5f; void OnEnable() => moveAction.Enable(); void OnDisable() => moveAction.Disable(); void Update() { // Fill in the code here } }
Look for the correct method to read the current value of an InputAction.
The method ReadValue<T>() reads the current value of the InputAction. Then multiplying by speed and deltaTime moves the character smoothly.