【问题标题】:Object won't stop moving. Using New Input System物体不会停止移动。使用新的输入系统
【发布时间】: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.xmovementInput.y 的值吗?
  • 它们输出 [-1,1] 值。我的问题一定是释放按钮,因为它们不会重置回 x:0 y:0。

标签: c# unity3d


【解决方案1】:

我遇到了同样的问题。以前我将 PlayerInput 与 UnityEvents 一起使用,而 WASD Move 工作得很好。后来我想改成 C# 事件,它就像你的代码:只使用 performed 委托。

深入研究 PlayerInput 源,我注意到 UnityEvents 在所有三个 started/performed/canceled 委托中都注册了。

解决方案:

_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 动作,没有它也可以。

【讨论】:

  • 由于我对新输入系统的经验有限,我不相信它是最好的通用解决方案,但至少对于这个特定问题它工作得很好
【解决方案2】:

所以是的,正如我提到的,我可以添加 Press 交互。

移动 动作 -> WASD 绑定 -> 交互中的“+”和按下。然后将 Trigger Behavior 设置为 Press And Release

我开车远离这个,因为如果我按我的方式通过多个 WASD 键,它会一直朝着按下的第一个键的方向移动。我会单独查找。

【讨论】:

  • 您找到解决多按键问题的方法了吗?
猜你喜欢
  • 2013-11-09
  • 2022-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多