英文:
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]'
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]'
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论