unity3d Unity输入系统移动

at0kjp5o  于 2023-06-30  发布在  其他
关注(0)|答案(1)|浏览(190)

我得到这个错误:InvalidOperationException:无法从绑定到操作“Player/Movement[/Keyboard/w,/Keyboard/a,/Keyboard/s,/Keyboard/d]”的控件“/Keyboard/w”中读取类型为“Vector 2”的值(控件是值类型为“float”的“KeyControl”)
还有这个执行“Player/Movement[/Keyboard/w,/Keyboard/a,/Keyboard/s,/Keyboard/d]”的“performed”回调时出现InvalidOperationException
errorsinput actions
下面是代码

using UnityEngine.InputSystem;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    private CustomInput input = null;
    private Vector2 moveVector = Vector2.zero;
    private Rigidbody2D rb = null;
    private float moveSpeed = 10f;

    private void Awake() {
        input = new CustomInput();
        rb = GetComponent<Rigidbody2D>();
    }

    private void OnEnable() {
        input.Enable();
        input.Player.Movement.performed += OnMovementPerformed;
        input.Player.Movement.canceled += OnMovementCancelled;
    }

    private void OnDisable() {
        input.Disable();
        input.Player.Movement.performed -= OnMovementPerformed;
        input.Player.Movement.canceled -= OnMovementCancelled;
    }

    private void FixedUpdate() {
        rb.velocity = moveVector * moveSpeed;
    }

    private void OnMovementPerformed(InputAction.CallbackContext value){
        moveVector = value.ReadValue<Vector2>();
    }

    private void OnMovementCancelled(InputAction.CallbackContext value){
        moveVector = Vector2.zero;
    }
}

如何解决这一问题?
我重新生成了项目文件,放入程序集定义Unity.InputSystem
对不起我的英语不好

eivgtgni

eivgtgni1#

该异常实际上是在告诉您正在尝试从具有float类型的输出值的控件中读取Vector2值。为了将输出值类型更改为Vector2,您的操作应该是复合类型。您可以下载InputSystem提供的一个示例,以详细了解它并了解如何正确设置控件。

相关问题