【发布时间】:2020-06-30 00:12:58
【问题描述】:
我一直在浏览文档以及与新输入系统相关的任何内容。我知道它是相当新的,并且在 1.0.0 上有很多变化。
经过长时间的休息后,我才刚刚开始使用 Unity,开始时我一直在尝试移动 Player。我让它工作了,但释放键盘键后它并没有停止。
一开始我并没有改变默认的 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();
}
}
【问题讨论】:
-
您能debug 并检查
movementInput.x和movementInput.y的值吗? -
它们输出 [-1,1] 值。我的问题一定是释放按钮,因为它们不会重置回 x:0 y:0。