Unity输入系统移动

huangapple go评论121阅读模式
英文:

Unity Input System Movement

问题

I get this error:
InvalidOperationException: Cannot read value of type 'Vector2' from control '/Keyboard/w' bound to action 'Player/Movement[/Keyboard/w,/Keyboard/a,/Keyboard/s,/Keyboard/d]' (control is a 'KeyControl' with value type 'float')

and also this:
InvalidOperationException while executing 'performed' callbacks of 'Player/Movement[/Keyboard/w,/Keyboard/a,/Keyboard/s,/Keyboard/d]'

errors
input actions

Here is the code:

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

How to solve this problem?

I regenerated project files, put in assembly definition Unity.InputSystem.

Sorry for my bad English.

英文:

I get this error:
InvalidOperationException: Cannot read value of type 'Vector2' from control '/Keyboard/w' bound to action 'Player/Movement[/Keyboard/w,/Keyboard/a,/Keyboard/s,/Keyboard/d]' (control is a 'KeyControl' with value type 'float')

and also this:
InvalidOperationException while executing 'performed' callbacks of 'Player/Movement[/Keyboard/w,/Keyboard/a,/Keyboard/s,/Keyboard/d]'

errors
input actions

Here is the code

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


How to solve this problem?

I regenerated project files, put in assembly definition Unity.InputSystem

sorry for my bad english

答案1

得分: 0

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

英文:

The exception is literally telling that you're trying to read a Vector2 value from a control that has an output value of type float. In order to change the output value type to Vector2 your action should be of a composite type.
You can download a sample provided with InputSystem to explore it in detail and understand how to set up controls properly.

huangapple
  • 本文由 发表于 2023年6月29日 15:11:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76578780.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定