首页 > 解决方案 > 物体不会停止移动。使用新的输入系统

问题描述

我一直在浏览文档以及与新输入系统相关的任何内容。我知道它是相当新的,并且在 1.0.0 上有很多变化。

经过长时间的休息后,我才刚刚开始使用 Unity,并且开始时我一直在尝试移动玩家。我让它工作,但释放键盘键后它并没有停止。

一开始我没有改变默认的 InputActions 设置。

默认输入操作设置

我确实尝试将交互更改为Press > Press & Release并且我得到了它,但是如果我按下了正确的键,例如,并在更改键(方向)时按住它,它会继续朝着正确的方向移动。我退出了这个,因为它说只使用“按钮”动作类型,但它改变了 WASD 的整个设置。

带有 Press & Release 的 InputActions 设置

这是我的播放器脚本。它可能与输入系统无关,与我的代码有关:

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

标签: c#unity3d

解决方案


我遇到了同样的问题。以前我将 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动作,没有它也可以工作。


推荐阅读