unity type_error ai_generated true

无效操作异常:无法从值类型为 'Vector2' 的控件读取类型为 'float' 的值。

InvalidOperationException: Cannot read value of type 'float' from a control with value type 'Vector2'.

ID: unity/input-system-float-parameter-invalid

其他格式: JSON · Markdown 中文 · English
90%修复率
85%置信度
1证据数
2023-08-12首次发现

版本兼容性

版本状态引入弃用备注
Unity 2022.3.12f1 active
Unity 2023.2.0b1 active
Input System 1.7.0 active

根因分析

输入系统操作绑定到了输出 Vector2 的控件(例如摇杆),但代码试图将其作为单个 float 读取,导致类型不匹配。

English

The Input System action is bound to a control that outputs a Vector2 (e.g., a joystick), but the code tries to read it as a single float, causing a type mismatch.

generic

官方文档

https://docs.unity3d.com/Packages/[email protected]/manual/ActionBindings.html

解决方案

  1. 将操作的预期值类型更改为与控件匹配。对于 Vector2 控件,使用 `action.ReadValue<Vector2>()` 读取 Vector2 值。例如:`Vector2 move = playerInput.actions["Move"].ReadValue<Vector2>();`。
  2. 如果只需要一个轴,则将操作绑定到单轴控件(例如键盘按键或按钮),而不是 Vector2 摇杆,并作为 float 读取。
  3. 仅当操作绑定到输出单个 float 的控件(例如 Axis 控件)时,才使用 `action.ReadValue<float>()`。在输入操作资源编辑器中验证绑定。

无效尝试

常见但无效的做法:

  1. 60% 失败

    Changing the action type to 'Pass Through' without adjusting the control binding may still result in type mismatch if the control output type is incompatible.

  2. 90% 失败

    Disabling and re-enabling the action map does not resolve the fundamental type mismatch between the expected value type and the control's output.