如何将Input.GetAxis("Mouse X")或Input.GetAxis("Mouse Y")转换为新的输入系统?

jus*_*fee 2 unity-game-engine

我是 Unity 脚本的新手,我正在尝试制作 ThirdPersonCamera,因此按照本教程,他可以正确地上下左右移动鼠标

在此输入图像描述

使用的脚本是

posY += Input.GetAxis("Mouse X") * mouseSensitivity;
posX -= Input.GetAxis("Mouse Y") * mouseSensitivity;
// mouseSensitivity can be changed

Vector3 targetRotation = new Vector3(posX, posY);
transform.eulerAngles = targetRotation;
Run Code Online (Sandbox Code Playgroud)

由于新的输入系统,使用Input.GetAxis("Mouse X")会引发InvalidOperationException: You are trying to read Input using the UnityEngine.Input class, but you have switched active Input handling to Input System package in Player Settings错误。

所以我尝试使用

private PlayerInputMovement inputCamera;

void Awake(){
    inputCamera = new PlayerInputMovement();
    inputCamera.Player.Camera.performed += context => cameraInput = context.ReadValue<Vector2>();
}

void Update(){
    Camera();
}

void Camera(){ 
    posY += cameraInput.x * mouseSensitivity;
    posX -= cameraInput.y * mouseSensitivity;
    // mouseSensitivity can be changed

    Vector3 targetRotation = new Vector3(posX, posY);
    transform.eulerAngles = targetRotation;
}
Run Code Online (Sandbox Code Playgroud)

明白了,但是如果我将鼠标保持在一个轴上,它就会不断旋转到那一侧

在此输入图像描述

那么...这是替换旧输入系统Input.GetAxis("Mouse X")Input.GetAxis("Mouse Y")新输入系统的正确方法吗?我怎样才能阻止它继续旋转?

shi*_*ngo 8

Mouse.current.delta.x.ReadValue()
Mouse.current.delta.y.ReadValue()
Run Code Online (Sandbox Code Playgroud)