无法从控件中读取“float"类型的值 [英] Cannot read value of type 'float' from control

查看:41
本文介绍了无法从控件中读取“float"类型的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的 Dual Shock 4 控制器映射到新的输入系统,但出现此错误:

I'm trying to map my Dual Shock 4 controller with the new input system, but I'm getting this error:

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 键有效,但我的控制器无效.

The WASD keys works but my controller does not.

这是我的脚本:

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();
    }
}

这是我的配置:

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

I cannot found any solution on the web. Seems like I'm the first one with this problem.

推荐答案

要补充上面的答案 - 为了让 Sticks 注册为绑定的可用输入,Action 还必须设置为Pass Through"和矢量 2"

To add to the answer above - to get the Sticks to register as available input for a binding, the Action also has to be set as "Pass Through" and "Vector 2"

控制器设置示例

这篇关于无法从控件中读取“float"类型的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆