unity-input-correctness
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseInput System (New) -- Correctness Patterns
新输入系统(Input System)——正确使用模式
Prerequisite skills:(Input System API, actions, bindings, PlayerInput component)unity-input
These patterns target the most common Input System bugs: wrong reading method for the action type, mixing old/new APIs, losing rebindings, and mishandling multiplayer device assignment.
必备技能:(Input System API、动作、绑定、PlayerInput组件)unity-input
这些模式针对输入系统最常见的Bug:动作类型读取方法错误、新旧API混用、重绑定丢失、多人设备分配处理不当等。
PATTERN: Reading Input -- triggered vs IsPressed vs WasPressedThisFrame
模式:输入读取——triggered vs IsPressed vs WasPressedThisFrame
WHEN: Reading button/action state at runtime
WRONG (Claude default):
csharp
// Using .triggered for continuous input (only fires once per press)
if (fireAction.triggered)
rb.AddForce(Vector3.forward * force); // Only fires one frame, not while held
// Using .IsPressed() for one-shot actions (fires every frame while held)
if (jumpAction.IsPressed())
Jump(); // Jumps every frame the button is held!RIGHT:
csharp
// One-shot actions (jump, interact, fire single bullet):
if (jumpAction.WasPressedThisFrame()) // True for exactly ONE frame
Jump();
// Or use .triggered (same as WasPressedThisFrame for Button actions with default interaction)
if (jumpAction.triggered)
Jump();
// Continuous actions (sprint, aim, hold to charge):
if (sprintAction.IsPressed()) // True every frame while held
moveSpeed = sprintSpeed;
// Value reading (stick, mouse delta):
Vector2 moveInput = moveAction.ReadValue<Vector2>(); // Continuous valueGOTCHA: respects Interactions (Hold, Tap, etc.) -- it fires when the interaction completes. fires on raw press regardless of interactions. returns true every frame while actuated above the press threshold. For type actions without interactions, == . For type actions, fires when the value changes from zero to non-zero.
.triggered.WasPressedThisFrame().IsPressed()Button.triggered.WasPressedThisFrame()Value.triggeredWHEN:运行时读取按钮/动作状态
WRONG(Claude默认写法):
csharp
// Using .triggered for continuous input (only fires once per press)
if (fireAction.triggered)
rb.AddForce(Vector3.forward * force); // Only fires one frame, not while held
// Using .IsPressed() for one-shot actions (fires every frame while held)
if (jumpAction.IsPressed())
Jump(); // Jumps every frame the button is held!RIGHT:
csharp
// One-shot actions (jump, interact, fire single bullet):
if (jumpAction.WasPressedThisFrame()) // True for exactly ONE frame
Jump();
// Or use .triggered (same as WasPressedThisFrame for Button actions with default interaction)
if (jumpAction.triggered)
Jump();
// Continuous actions (sprint, aim, hold to charge):
if (sprintAction.IsPressed()) // True every frame while held
moveSpeed = sprintSpeed;
// Value reading (stick, mouse delta):
Vector2 moveInput = moveAction.ReadValue<Vector2>(); // Continuous valueGOTCHA:会遵循Interactions(按住、点击等)规则——仅在交互完成时触发。会在原始按下操作发生时触发,不受Interactions影响。在操作超过按压阈值的每帧都会返回true。对于未设置Interactions的Button类型动作,与效果一致。对于Value类型动作,会在值从0变为非0时触发。
.triggered.WasPressedThisFrame().IsPressed().triggered.WasPressedThisFrame().triggeredPATTERN: Action Map Switching
模式:动作映射切换
WHEN: Switching between action maps (e.g., Gameplay -> UI -> Vehicle)
WRONG (Claude default):
csharp
// Forgetting that SwitchCurrentActionMap disables the previous map
playerInput.SwitchCurrentActionMap("UI");
// All "Gameplay" actions are now DISABLED -- callbacks won't fire
// If you cached Gameplay actions, they silently stop workingRIGHT:
csharp
// Option 1: Via PlayerInput (handles enable/disable automatically)
playerInput.SwitchCurrentActionMap("UI");
// Previous map disabled, new map enabled
// Option 2: Manual enable/disable (more control)
gameplayActions.Disable();
uiActions.Enable();
// Option 3: Keep both maps active simultaneously
// (useful for universal actions like Pause)
gameplayActions.Enable();
pauseActions.Enable(); // Both active at onceGOTCHA: When using , the previous map is fully disabled. Any cached references from the previous map stop firing callbacks until re-enabled. If you need certain actions (like Pause) to work across all maps, put them in a separate map that stays enabled, or use the manual enable/disable approach.
PlayerInput.SwitchCurrentActionMapInputActionWHEN:在动作映射间切换(例如:游戏玩法→UI→载具)
WRONG(Claude默认写法):
csharp
// Forgetting that SwitchCurrentActionMap disables the previous map
playerInput.SwitchCurrentActionMap("UI");
// All "Gameplay" actions are now DISABLED -- callbacks won't fire
// If you cached Gameplay actions, they silently stop workingRIGHT:
csharp
// Option 1: Via PlayerInput (handles enable/disable automatically)
playerInput.SwitchCurrentActionMap("UI");
// Previous map disabled, new map enabled
// Option 2: Manual enable/disable (more control)
gameplayActions.Disable();
uiActions.Enable();
// Option 3: Keep both maps active simultaneously
// (useful for universal actions like Pause)
gameplayActions.Enable();
pauseActions.Enable(); // Both active at onceGOTCHA:使用时,之前的映射会被完全禁用。从之前映射缓存的所有引用会停止触发回调,直到重新启用。如果需要某些动作(如暂停)在所有映射中都能生效,可将其放在单独的映射中保持启用状态,或使用手动启用/禁用的方式。
PlayerInput.SwitchCurrentActionMapInputActionPATTERN: Processor vs Interaction Confusion
模式:Processor与Interaction使用混淆
WHEN: Applying deadzones or modifying input values
WRONG (Claude default):
csharp
// Adding a deadzone as an Interaction (Interactions modify TIMING, not values)
// In .inputactions: Action > Interactions > "Deadzone" -- this doesn't exist as an interactionRIGHT:
csharp
// Deadzones are PROCESSORS -- they modify the input VALUE
// Set in .inputactions: Binding > Processors > "Stick Deadzone" or "Axis Deadzone"
// Processors modify the value stream: Raw Input -> Processor Chain -> Final Value
// Common processors:
// StickDeadzone -- applies radial deadzone to Vector2 (sticks)
// AxisDeadzone -- applies linear deadzone to float (triggers)
// Normalize -- normalizes Vector2 to 0-1 range
// Invert -- negates the value
// Scale -- multiplies by a factor
// Clamp -- clamps to min/max range
// Runtime processor override (if needed):
moveAction.ApplyBindingOverride(new InputBinding { overrideProcessors = "StickDeadzone(min=0.2,max=0.9)" });GOTCHA: Processors transform the value (deadzone, normalize, scale, invert). Interactions change the timing of when // fire (Press, Hold, Tap, SlowTap, MultiTap). Confusing them results in either no deadzone (processor missing) or wrong callback timing (interaction added where not needed).
startedperformedcanceledWHEN:应用死区或修改输入值
WRONG(Claude默认写法):
csharp
// Adding a deadzone as an Interaction (Interactions modify TIMING, not values)
// In .inputactions: Action > Interactions > "Deadzone" -- this doesn't exist as an interactionRIGHT:
csharp
// Deadzones are PROCESSORS -- they modify the input VALUE
// Set in .inputactions: Binding > Processors > "Stick Deadzone" or "Axis Deadzone"
// Processors modify the value stream: Raw Input -> Processor Chain -> Final Value
// Common processors:
// StickDeadzone -- applies radial deadzone to Vector2 (sticks)
// AxisDeadzone -- applies linear deadzone to float (triggers)
// Normalize -- normalizes Vector2 to 0-1 range
// Invert -- negates the value
// Scale -- multiplies by a factor
// Clamp -- clamps to min/max range
// Runtime processor override (if needed):
moveAction.ApplyBindingOverride(new InputBinding { overrideProcessors = "StickDeadzone(min=0.2,max=0.9)" });GOTCHA:Processors用于转换输入值(死区、归一化、缩放、反转)。Interactions用于改变//事件的触发时机(按压、按住、点击、慢点击、多次点击)。混淆两者会导致死区不生效(缺少Processor)或回调时机错误(错误添加了Interaction)。
startedperformedcanceledPATTERN: InputValue Lifetime in SendMessages/BroadcastMessages
模式:SendMessages/BroadcastMessages中的InputValue生命周期
WHEN: Using in SendMessages or BroadcastMessages behavior mode
PlayerInputWRONG (Claude default):
csharp
private InputValue _cachedInput; // Storing the reference
void OnMove(InputValue value)
{
_cachedInput = value; // WRONG: InputValue is pooled and recycled
}
void Update()
{
Vector2 dir = _cachedInput.Get<Vector2>(); // May return stale or corrupt data
}RIGHT:
csharp
private Vector2 _moveInput;
void OnMove(InputValue value)
{
// Copy the value immediately -- InputValue is only valid during the callback
_moveInput = value.Get<Vector2>();
}
void Update()
{
transform.Translate(_moveInput * speed * Time.deltaTime);
}GOTCHA: is a wrapper that is reused between callbacks. Its internal data is only valid during the callback invocation. Always copy with in the callback and store the result. This applies to SendMessages and BroadcastMessages modes. UnityEvents and C# Events modes don't use -- they pass which has the same lifetime constraint.
InputValue.Get<T>()InputValueInputAction.CallbackContextWHEN:在SendMessages或BroadcastMessages行为模式下使用
PlayerInputWRONG(Claude默认写法):
csharp
private InputValue _cachedInput; // Storing the reference
void OnMove(InputValue value)
{
_cachedInput = value; // WRONG: InputValue is pooled and recycled
}
void Update()
{
Vector2 dir = _cachedInput.Get<Vector2>(); // May return stale or corrupt data
}RIGHT:
csharp
private Vector2 _moveInput;
void OnMove(InputValue value)
{
// Copy the value immediately -- InputValue is only valid during the callback
_moveInput = value.Get<Vector2>();
}
void Update()
{
transform.Translate(_moveInput * speed * Time.deltaTime);
}GOTCHA:是一个会被复用的包装类,其内部数据仅在回调执行期间有效。务必在回调中使用复制值并保存结果。这一规则适用于SendMessages和BroadcastMessages模式。UnityEvents和C# Events模式不使用——它们传递的同样有生命周期限制。
InputValue.Get<T>()InputValueInputAction.CallbackContextPATTERN: PassThrough vs Value for Multi-Source Input
模式:多源输入的PassThrough vs Value模式
WHEN: Handling input from multiple simultaneous sources (multi-touch, multiple gamepads)
WRONG (Claude default):
csharp
// Using "Value" action type for multi-touch
// Value type performs disambiguation -- picks the input with highest magnitude
// You only see ONE touch, even if multiple fingers are on screenRIGHT:
csharp
// Use "PassThrough" action type for all-source input
// PassThrough does NOT disambiguate -- every input source triggers the action
// In .inputactions file: Set Action Type = "Pass Through"
// This is essential for:
// - Multi-touch (each finger fires separately)
// - Multiple gamepads sending the same action
// - Combining keyboard + mouse simultaneously
// Read which device triggered it:
void OnAction(InputAction.CallbackContext ctx)
{
var device = ctx.control.device;
var value = ctx.ReadValue<float>();
}GOTCHA: Button: fires on press/release, returns float 0 or 1. Value: fires when value changes, picks highest-magnitude source (disambiguation). PassThrough: fires on every change from every source, no disambiguation. For most gameplay input, is correct. Use only when you need per-device or per-finger tracking.
ValuePassThroughWHEN:处理多个同时输入源(多点触控、多个游戏手柄)
WRONG(Claude默认写法):
csharp
// Using "Value" action type for multi-touch
// Value type performs disambiguation -- picks the input with highest magnitude
// You only see ONE touch, even if multiple fingers are on screenRIGHT:
csharp
// Use "PassThrough" action type for all-source input
// PassThrough does NOT disambiguate -- every input source triggers the action
// In .inputactions file: Set Action Type = "Pass Through"
// This is essential for:
// - Multi-touch (each finger fires separately)
// - Multiple gamepads sending the same action
// - Combining keyboard + mouse simultaneously
// Read which device triggered it:
void OnAction(InputAction.CallbackContext ctx)
{
var device = ctx.control.device;
var value = ctx.ReadValue<float>();
}GOTCHA:Button:按下/释放时触发,返回0或1的浮点数。Value:值变化时触发,选择幅度最大的输入源(消歧)。PassThrough:每个输入源的每次变化都会触发,不消歧。对于大多数游戏玩法输入,模式是正确的。仅当需要跟踪每个设备或每个手指的输入时,才使用模式。
ValuePassThroughPATTERN: Action Enable/Disable Scope
模式:动作启用/禁用范围
WHEN: Enabling/disabling individual actions vs entire action maps
WRONG (Claude default):
csharp
// Enabling an action without enabling its map
fireAction.Enable(); // Works, BUT...
// If the map was disabled, this implicitly enables JUST this action
// Other actions in the same map remain disabledRIGHT:
csharp
// Preferred: Enable/disable at the MAP level
playerActions.Enable(); // Enables all actions in the map
playerActions.Disable(); // Disables all actions
// Individual action enable/disable (advanced use only):
fireAction.Enable(); // Enables this action even if map is disabled
fireAction.Disable(); // Disables only this action
// Check state:
bool mapEnabled = playerActions.enabled;
bool actionEnabled = fireAction.enabled;GOTCHA: An action can be enabled while its containing map is "disabled" -- the action still works. But this creates confusing state: returns false while returns true. Best practice: always enable/disable at the map level. Only use per-action enable/disable for special cases like temporarily disabling fire while reloading.
map.enabledaction.enabledWHEN:启用/禁用单个动作与整个动作映射
WRONG(Claude默认写法):
csharp
// Enabling an action without enabling its map
fireAction.Enable(); // Works, BUT...
// If the map was disabled, this implicitly enables JUST this action
// Other actions in the same map remain disabledRIGHT:
csharp
// Preferred: Enable/disable at the MAP level
playerActions.Enable(); // Enables all actions in the map
playerActions.Disable(); // Disables all actions
// Individual action enable/disable (advanced use only):
fireAction.Enable(); // Enables this action even if map is disabled
fireAction.Disable(); // Disables only this action
// Check state:
bool mapEnabled = playerActions.enabled;
bool actionEnabled = fireAction.enabled;GOTCHA:动作可以在其所属映射“禁用”的状态下单独启用——该动作仍可正常工作。但这会造成混淆的状态:返回false,而返回true。最佳实践:始终在映射级别进行启用/禁用操作。仅在特殊场景下使用单个动作的启用/禁用,例如换弹时临时禁用开火动作。
map.enabledaction.enabledPATTERN: Device-Specific Button Prompts
模式:设备特定按钮提示
WHEN: Displaying control hints to the player (e.g., "Press X to interact")
WRONG (Claude default):
csharp
// Hardcoded button names
promptText.text = "Press A to Jump";
// Wrong on keyboard (should be "Space"), PS5 (should be "Cross"), etc.RIGHT:
csharp
// Get the display name for the current binding
InputAction jumpAction = inputActions.FindAction("Jump");
// Get display string for the active control scheme
string displayName = jumpAction.GetBindingDisplayString(
InputBinding.DisplayStringOptions.DontOmitDevice);
promptText.text = $"Press {displayName} to Jump";
// For a specific control scheme:
int bindingIndex = jumpAction.GetBindingIndex(
InputBinding.MaskByGroup("Gamepad"));
if (bindingIndex >= 0)
{
string gamepadPrompt = jumpAction.GetBindingDisplayString(bindingIndex);
// Returns "Button South" or device-specific name
}GOTCHA: returns human-readable names. Without parameters, it returns the string for the first binding. Use binding masks or indices to target specific control schemes. For full icon support, you need a custom or asset that maps control paths to sprite/icon references -- Unity does not provide built-in icon mapping.
GetBindingDisplayString()InputBindingCompositeWHEN:向玩家显示控制提示(例如:“Press X to interact”)
WRONG(Claude默认写法):
csharp
// Hardcoded button names
promptText.text = "Press A to Jump";
// Wrong on keyboard (should be "Space"), PS5 (should be "Cross"), etc.RIGHT:
csharp
// Get the display name for the current binding
InputAction jumpAction = inputActions.FindAction("Jump");
// Get display string for the active control scheme
string displayName = jumpAction.GetBindingDisplayString(
InputBinding.DisplayStringOptions.DontOmitDevice);
promptText.text = $"Press {displayName} to Jump";
// For a specific control scheme:
int bindingIndex = jumpAction.GetBindingIndex(
InputBinding.MaskByGroup("Gamepad"));
if (bindingIndex >= 0)
{
string gamepadPrompt = jumpAction.GetBindingDisplayString(bindingIndex);
// Returns "Button South" or device-specific name
}GOTCHA:返回人类可读的名称。不带参数时,返回第一个绑定的字符串。使用绑定掩码或索引可针对特定控制方案。如需完整图标支持,需自定义或使用能将控制路径映射到精灵/图标的资源——Unity未提供内置图标映射功能。
GetBindingDisplayString()InputBindingCompositePATTERN: Local Multiplayer Device Assignment
模式:本地多人设备分配
WHEN: Supporting multiple players on the same machine with separate controllers
WRONG (Claude default):
csharp
// Both players reading from the same static device reference
Vector2 p1Move = Gamepad.current.leftStick.ReadValue();
Vector2 p2Move = Gamepad.current.leftStick.ReadValue(); // Same gamepad!RIGHT:
csharp
// Use PlayerInputManager for automatic device assignment
// 1. Add PlayerInputManager component to a manager object
// 2. Set Join Behavior (e.g., JoinPlayersWhenButtonIsPressed)
// 3. Set Player Prefab (must have PlayerInput component)
// PlayerInputManager automatically assigns unique devices to each player
// In the player script:
public class PlayerController : MonoBehaviour
{
private PlayerInput _playerInput;
private InputAction _moveAction;
void Awake()
{
_playerInput = GetComponent<PlayerInput>();
_moveAction = _playerInput.actions["Move"];
}
void Update()
{
// Each PlayerInput instance reads from its ASSIGNED device only
Vector2 move = _moveAction.ReadValue<Vector2>();
transform.Translate(move * speed * Time.deltaTime);
}
}
// Listen for join/leave events:
void OnEnable()
{
PlayerInputManager.instance.onPlayerJoined += OnPlayerJoined;
PlayerInputManager.instance.onPlayerLeft += OnPlayerLeft;
}GOTCHA: returns the most recently used gamepad -- NOT a specific player's gamepad. For multiplayer, always read input through the component which manages device assignment. limits players. Split-screen is handled via assignment -- each player gets a camera with a different viewport rect.
Gamepad.currentPlayerInputPlayerInputManager.instance.maxPlayerCountPlayerInput.cameraWHEN:支持同一台机器上多个玩家使用不同控制器
WRONG(Claude默认写法):
csharp
// Both players reading from the same static device reference
Vector2 p1Move = Gamepad.current.leftStick.ReadValue();
Vector2 p2Move = Gamepad.current.leftStick.ReadValue(); // Same gamepad!RIGHT:
csharp
// Use PlayerInputManager for automatic device assignment
// 1. Add PlayerInputManager component to a manager object
// 2. Set Join Behavior (e.g., JoinPlayersWhenButtonIsPressed)
// 3. Set Player Prefab (must have PlayerInput component)
// PlayerInputManager automatically assigns unique devices to each player
// In the player script:
public class PlayerController : MonoBehaviour
{
private PlayerInput _playerInput;
private InputAction _moveAction;
void Awake()
{
_playerInput = GetComponent<PlayerInput>();
_moveAction = _playerInput.actions["Move"];
}
void Update()
{
// Each PlayerInput instance reads from its ASSIGNED device only
Vector2 move = _moveAction.ReadValue<Vector2>();
transform.Translate(move * speed * Time.deltaTime);
}
}
// Listen for join/leave events:
void OnEnable()
{
PlayerInputManager.instance.onPlayerJoined += OnPlayerJoined;
PlayerInputManager.instance.onPlayerLeft += OnPlayerLeft;
}GOTCHA:返回最近使用的游戏手柄——并非特定玩家的游戏手柄。对于多人游戏,务必通过组件读取输入,该组件会管理设备分配。用于限制玩家数量。分屏显示通过分配处理——每个玩家会获得一个带有不同视口矩形的相机。
Gamepad.currentPlayerInputPlayerInputManager.instance.maxPlayerCountPlayerInput.cameraPATTERN: Control Scheme Auto-Switching
模式:控制方案自动切换
WHEN: Players switch between keyboard and gamepad mid-game
WRONG (Claude default):
csharp
// Assuming the control scheme is fixed after startup
// UI shows keyboard prompts even after player picks up a gamepadRIGHT:
csharp
public class ControlSchemeHandler : MonoBehaviour
{
private PlayerInput _playerInput;
void OnEnable()
{
_playerInput = GetComponent<PlayerInput>();
_playerInput.controlsChangedEvent.AddListener(OnControlsChanged);
// Initialize with current scheme
UpdatePrompts(_playerInput.currentControlScheme);
}
void OnDisable()
{
_playerInput.controlsChangedEvent.RemoveListener(OnControlsChanged);
}
void OnControlsChanged(PlayerInput input)
{
UpdatePrompts(input.currentControlScheme);
}
void UpdatePrompts(string schemeName)
{
bool isGamepad = schemeName == "Gamepad";
// Update UI prompts, button icons, etc.
promptIcon.sprite = isGamepad ? gamepadSprite : keyboardSprite;
}
}GOTCHA: auto-switches control schemes when it detects input from a different device type. fires on every switch. returns the name string matching your control scheme names. The switch happens on the next input event, not immediately on device connection. Test with both devices plugged in simultaneously.
PlayerInputcontrolsChangedEventcurrentControlScheme.inputactionsWHEN:玩家在游戏中途在键盘和游戏手柄间切换
WRONG(Claude默认写法):
csharp
// Assuming the control scheme is fixed after startup
// UI shows keyboard prompts even after player picks up a gamepadRIGHT:
csharp
public class ControlSchemeHandler : MonoBehaviour
{
private PlayerInput _playerInput;
void OnEnable()
{
_playerInput = GetComponent<PlayerInput>();
_playerInput.controlsChangedEvent.AddListener(OnControlsChanged);
// Initialize with current scheme
UpdatePrompts(_playerInput.currentControlScheme);
}
void OnDisable()
{
_playerInput.controlsChangedEvent.RemoveListener(OnControlsChanged);
}
void OnControlsChanged(PlayerInput input)
{
UpdatePrompts(input.currentControlScheme);
}
void UpdatePrompts(string schemeName)
{
bool isGamepad = schemeName == "Gamepad";
// Update UI prompts, button icons, etc.
promptIcon.sprite = isGamepad ? gamepadSprite : keyboardSprite;
}
}GOTCHA:当检测到不同设备类型的输入时,会自动切换控制方案。会在每次切换时触发。返回与中控制方案名称匹配的字符串。切换会在下一次输入事件时发生,而非设备连接后立即触发。需同时连接两种设备进行测试。
PlayerInputcontrolsChangedEventcurrentControlScheme.inputactionsAnti-Patterns Quick Reference
反模式快速参考
| Anti-Pattern | Problem | Fix |
|---|---|---|
| Old and new API conflict; may require both backends active | Fully migrate to new Input System; remove |
Not calling | Action does nothing; no errors | Enable action map or individual action before reading |
Reading | Returns default value silently | Match |
Forgetting to dispose | Memory leak | Always call |
Using legacy | Mixes IMGUI with Input System | Use UI Toolkit or Input System callbacks |
| Not saving rebind overrides | Players lose custom bindings on restart | Save with |
| 反模式 | 问题 | 修复方案 |
|---|---|---|
| 新旧API冲突;可能需要同时启用两个后端 | 完全迁移到新输入系统;移除 |
未调用 | 动作无响应;无报错信息 | 在读取前启用动作映射或单个动作 |
使用错误类型 | 静默返回默认值 | 确保 |
未销毁 | 内存泄漏 | 在 |
使用旧版 | IMGUI与输入系统混用 | 使用UI Toolkit或输入系统回调 |
| 未保存重绑定覆盖设置 | 玩家重启后丢失自定义绑定 | 使用 |
Related Skills
相关技能
- unity-input -- Input System API reference, action types, binding syntax, device access
- unity-ui -- UI Toolkit input handling, navigation events
- unity-multiplayer -- Netcode input authority, client prediction
- unity-input —— 输入系统API参考、动作类型、绑定语法、设备访问
- unity-ui —— UI Toolkit输入处理、导航事件
- unity-multiplayer —— 网络代码输入权限、客户端预测