如何修复玩家移动漂移?

How to fix player movement drifting?

每当我移动(上、下、左、右)时,运动都会有漂移。例如,当我向前移动然后停止按下按键时,即使我停止按下,播放器也会继续朝我移动的相同方向移动! [我正在使用新的输入系统]。

     [SerializeField] Movement movement;
     [SerializeField] PlayerInteractions playerInteractions;
     [SerializeField] MouseLook mouseLook;
     PlayerControls.GroundMovementActions groundMovement;
     Vector2 horizontalInput;
     Vector2 mouseInput;
    private void Awake() 
  {
        controls = new PlayerControls(); 
        groundMovement = controls.GroundMovement;
        // groundMovement.[action].performed += context => do something
        groundMovement.HorizontalMovement.performed += ctx => horizontalInput = ctx.ReadValue<Vector2>();
        groundMovement.MouseX.performed += ctx => mouseInput.x = ctx.ReadValue<float>();
        groundMovement.MouseY.performed += ctx => mouseInput.y = ctx.ReadValue<float>();       
    }   

    private void Update()
    {
        movement.ReceiveInput(horizontalInput);
        mouseLook.ReceiveInput(mouseInput); 
    }

Movement class:

  private void stateHandler() 
    {
        
        isGrounded = Physics.CheckSphere(transform.position, 0.1f, groundMask);
        if (isGrounded)
        {
            verticalVelocity.y = 0;   
        }
        // Jump: v = sqrt(-2 * jumpheight* gravity)
        if (jump)
        {
            state = MovementState.jump;
            if (isGrounded)
            {
                verticalVelocity.y = Mathf.Sqrt(-2f * jumpHeight * gravity);         
            }
            jump = false;

        }
        verticalVelocity.y += gravity * Time.deltaTime;
        controller.Move(verticalVelocity * Time.deltaTime);

        if (running)
        {
            state=MovementState.running;
            moveSpeed = runSpeed;
            horizontalVelocity = (transform.right * horizontalInput.x + transform.forward * horizontalInput.y) * moveSpeed;
            controller.Move(horizontalVelocity * Time.deltaTime);   
        }
        else
        {
            moveSpeed = walkSpeed;
            horizontalVelocity = (transform.right * horizontalInput.x + transform.forward * horizontalInput.y) * moveSpeed;
            controller.Move(horizontalVelocity * Time.deltaTime);
            state = MovementState.walking;
        }
}
  private void Update()
    {
        stateHandler();
    }

    public void ReceiveInput(Vector2 _horizontalInput)
    {
        horizontalInput = _horizontalInput;
    }

MouseLook class:

private void Update()
    {
        transform.Rotate(Vector3.up, mouseX * Time.fixedDeltaTime);
        xRotataion -= mouseY;
        xRotataion = Mathf.Clamp(xRotataion, -xClamp, xClamp);
        Vector3 targetRotation = transform.eulerAngles;
        targetRotation.x = xRotataion;
        playerCamera.eulerAngles = targetRotation;

    }
    public void ReceiveInput(Vector2 mouseInput) 
    {
        mouseX = mouseInput.x * sensitivityX;
        mouseY = mouseInput.y * sensitivityY;
    }

以下是我试图解决的问题:

groundMovement.MouseX.started += ctx => mouseInput.x = ctx.ReadValue<float>();
groundMovement.MouseX.canceled += ctx => mouseInput.x = ctx.ReadValue<float>();
groundMovement.MouseY.started += ctx => mouseInput.y = ctx.ReadValue<float>();
groundMovement.MouseY.canceled += ctx => mouseInput.y = ctx.ReadValue<float>();

但是没有用,每次都在漂移。我该如何解决这个问题?

你想让角色停止移动但是你已经改变了MouseXMouseY。添加以下代码。

groundMovement.HorizontalMovement.canceled += ctx => horizontalInput = Vector2.zero;