首页 > 解决方案 > 无法从控件中读取“浮点”类型的值

问题描述

我正在尝试将 Dual Shock 4 控制器映射到新的输入系统,但我收到了这个错误:

InvalidOperationException: Cannot read value of type 'float' from control '/DualShock4GamepadHID/leftStick' bound to action 'Gameplay/Movement[/Keyboard/w,/Keyboard/s,/Keyboard/a,/Keyboard/d,/DualShock4GamepadHID/leftStick,/DualShock4GamepadHID/leftStick,/DualShock4GamepadHID/leftStick,/DualShock4GamepadHID/leftStick]' (control is a 'StickControl' with value type 'Vector2')
UnityEngine.InputSystem.InputActionState.ReadValue[TValue] (System.Int32 bindingIndex, System.Int32 controlIndex, System.Boolean ignoreComposites) (at Library/PackageCache/com.unity.inputsystem@1.0.0-preview.1/InputSystem/Actions/InputActionState.cs:1929)
UnityEngine.InputSystem.InputActionState.ReadCompositePartValue[TValue,TComparer] (System.Int32 bindingIndex, System.Int32 partNumber, System.Boolean* buttonValuePtr, System.Int32& controlIndex, TComparer comparer) (at Library/PackageCache/com.unity.inputsystem@1.0.0-preview.1/InputSystem/Actions/InputActionState.cs:2010)
UnityEngine.InputSystem.InputBindingCompositeContext.ReadValueAsButton (System.Int32 partNumber) (at Library/PackageCache/com.unity.inputsystem@1.0.0-preview.1/InputSystem/Actions/InputBindingCompositeContext.cs:252)
UnityEngine.InputSystem.Composites.Vector2Composite.ReadValue (UnityEngine.InputSystem.InputBindingCompositeContext& context) (at Library/PackageCache/com.unity.inputsystem@1.0.0-preview.1/InputSystem/Actions/Composites/Vector2Composite.cs:72)
UnityEngine.InputSystem.Composites.Vector2Composite.EvaluateMagnitude (UnityEngine.InputSystem.InputBindingCompositeContext& context) (at Library/PackageCache/com.unity.inputsystem@1.0.0-preview.1/InputSystem/Actions/Composites/Vector2Composite.cs:83)
UnityEngine.InputSystem.InputActionState.ComputeMagnitude (System.Int32 bindingIndex, System.Int32 controlIndex) (at Library/PackageCache/com.unity.inputsystem@1.0.0-preview.1/InputSystem/Actions/InputActionState.cs:1826)
UnityEngine.InputSystem.InputActionState.ShouldIgnoreControlStateChange (UnityEngine.InputSystem.InputActionState+TriggerState& trigger, System.Int32 actionIndex) (at Library/PackageCache/com.unity.inputsystem@1.0.0-preview.1/InputSystem/Actions/InputActionState.cs:984)
UnityEngine.InputSystem.InputActionState.ProcessControlStateChange (System.Int32 mapIndex, System.Int32 controlIndex, System.Int32 bindingIndex, System.Double time, UnityEngine.InputSystem.LowLevel.InputEventPtr eventPtr) (at Library/PackageCache/com.unity.inputsystem@1.0.0-preview.1/InputSystem/Actions/InputActionState.cs:875)
UnityEngine.InputSystem.InputActionState.UnityEngine.InputSystem.LowLevel.IInputStateChangeMonitor.NotifyControlStateChanged (UnityEngine.InputSystem.InputControl control, System.Double time, UnityEngine.InputSystem.LowLevel.InputEventPtr eventPtr, System.Int64 mapControlAndBindingIndex) (at Library/PackageCache/com.unity.inputsystem@1.0.0-preview.1/InputSystem/Actions/InputActionState.cs:783)
UnityEngine.InputSystem.InputManager.FireStateChangeNotifications (System.Int32 deviceIndex, System.Double internalTime, UnityEngine.InputSystem.LowLevel.InputEvent* eventPtr) (at Library/PackageCache/com.unity.inputsystem@1.0.0-preview.1/InputSystem/InputManager.cs:2687)
UnityEngine.InputSystem.LowLevel.<>c__DisplayClass7_0:<set_onUpdate>b__0(NativeInputUpdateType, NativeInputEventBuffer*)
UnityEngineInternal.Input.NativeInputSystem:NotifyUpdate(NativeInputUpdateType, IntPtr) (at C:/buildslave/unity/build/Modules/Input/Private/Input.cs:120)

WASD 键有效,但我的控制器无效。

那是我的脚本:

using UnityEngine;
using UnityEngine.InputSystem;

public class CubeMovement : MonoBehaviour, PlayerControls.IGameplayActions
{
    private PlayerControls m_PlayerControls;
    private Vector2 m_Direction;
    [SerializeField] private float movementVelocity;

    private void Awake()
    {
        m_Direction = Vector2.zero;
        m_PlayerControls = new PlayerControls();
        m_PlayerControls.Gameplay.SetCallbacks(this);
    }

    private void Update()
    {
        var direction = Time.deltaTime * new Vector3(m_Direction.x, 0, m_Direction.y);
        transform.Translate(direction);
    }

    public void OnMovement(InputAction.CallbackContext context)
    {
        m_Direction = movementVelocity * context.ReadValue<Vector2>();
    }

    private void OnEnable()
    {
        m_PlayerControls.Gameplay.Enable();
    }


    private void OnDisable()
    {
        m_PlayerControls.Gameplay.Disable();
    }
}

那是我的配置:

Unity 输入动作配置


我在网上找不到任何解决方案。好像我是第一个遇到这个问题的人。

在此处输入图像描述

标签: c#unity3d

解决方案


要添加到上面的答案 - 要让 Sticks 注册为绑定的可用输入,还必须将 Action 设置为“Pass Through”和“Vector 2”

控制器设置示例


推荐阅读