c# - 物体不会停止移动。使用新的输入系统
问题描述
我一直在浏览文档以及与新输入系统相关的任何内容。我知道它是相当新的,并且在 1.0.0 上有很多变化。
经过长时间的休息后,我才刚刚开始使用 Unity,并且开始时我一直在尝试移动玩家。我让它工作,但释放键盘键后它并没有停止。
一开始我没有改变默认的 InputActions 设置。
我确实尝试将交互更改为Press > Press & Release并且我得到了它,但是如果我按下了正确的键,例如,并在更改键(方向)时按住它,它会继续朝着正确的方向移动。我退出了这个,因为它说只使用“按钮”动作类型,但它改变了 WASD 的整个设置。
这是我的播放器脚本。它可能与输入系统无关,与我的代码有关:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerBehaviour : MonoBehaviour
{
private InputActions _controls;
private Vector2 movementInput;
public float _speed = 12f;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
private void OnEnable()
{
_controls = new InputActions();
_controls.Player.Move.performed += ctx => movementInput = ctx.ReadValue<Vector2>();
_controls.Player.Move.Enable();
}
void Update()
{
Debug.Log("transform.position: " + transform.position);
transform.position += new Vector3(movementInput.x * _speed * Time.deltaTime,
0,
movementInput.y * _speed * Time.deltaTime);
}
private void OnDisable()
{
_controls.Player.Move.Disable();
}
}
更新:我一直在努力解决这个问题并添加了更多内容,但无法解决我原来的问题。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovement : MonoBehaviour
{
private InputActions _controls;
private Vector2 movementInput;
public float _speed = 12f;
public CharacterController controller;
public Camera playerCamera;
private Vector2 lookPosition;
private float mouseSensitivity = 50f;
float xRotation = 0f;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
private void OnEnable()
{
_controls = new InputActions();
_controls.Player.Move.performed += ctx => movementInput = ctx.ReadValue<Vector2>();
_controls.Player.Move.Enable();
_controls.Player.Look.performed += ctx => lookPosition = ctx.ReadValue<Vector2>();
_controls.Player.Look.Enable();
}
void Update()
{
float x = movementInput.x;
float z = movementInput.y;
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * _speed * Time.deltaTime);
float lookX = lookPosition.x * mouseSensitivity * Time.deltaTime;
float lookY = lookPosition.y * mouseSensitivity * Time.deltaTime;
xRotation -= lookPosition.y;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
playerCamera.transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
transform.Rotate(Vector3.up * lookX);
}
private void OnDisable()
{
_controls.Player.Move.Disable();
_controls.Player.Look.Disable();
}
}
解决方案
我遇到了同样的问题。以前我将 PlayerInput 与 UnityEvents 一起使用,而 WASD Move工作得很好。后来我想改成 C# 事件,它就像你的代码:只使用执行的委托。
深入研究 PlayerInput 源,我注意到 UnityEvents 在所有三个已启动/已执行/已取消的委托中都注册了。
解决方案:
_controls.Player.Move.started += ctx => movementInput = ctx.ReadValue<Vector2>()
_controls.Player.Move.performed += ctx => movementInput = ctx.ReadValue<Vector2>()
_controls.Player.Move.canceled += ctx => movementInput = ctx.ReadValue<Vector2>()
也不需要添加Press动作,没有它也可以工作。
推荐阅读
- python - 烧瓶应用程序启动后运行代码:生产模式
- domain-driven-design - 在 DDD 中,评论是否包含在帖子聚合中?
- java - Minecraft Modding 1.12.2 制作包含工具的制作配方
- algorithm - theta 表示法和 Big o 表示法之和
- javascript - 递归调用对象方法
- php - file_get_contents 未能打开流 HTTP 请求失败?
- spring - Spring Boot 反应式(webflux)监控与监控普通 Spring Boot MVC 有什么不同吗?
- javascript - 如何通过函数参数发送 POST 数据?
- excel - EXCEL 公式 - 索引匹配、大型和多个标准
- pandas - 如何根据循环的值添加数据框列