无法从控件读取“ float”类型的值

时间:2019-10-20 02:06:05

标签: c# unity3d

我正在尝试将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 Input Action configuration


我在网上找不到任何解决方案。 似乎我是第一个遇到此问题的人。

enter image description here

2 个答案:

答案 0 :(得分:1)

要添加到上面的答案中-要使操纵杆注册为可用于绑定的输入,还必须将操作设置为“通过”和“向量2”

Controller setup example

答案 1 :(得分:0)

我找到了解决方案。 绘制棍子的正确方法是使用简单绑定而不是2D矢量合成。

enter image description here