【问题标题】:How to convert Input.GetAxis("Mouse X") or Input.GetAxis("Mouse Y") to the new Input System?如何将 Input.GetAxis("Mouse X") 或 Input.GetAxis("Mouse Y") 转换为新的输入系统?
【发布时间】:2021-05-06 04:34:31
【问题描述】:

我是 Unity 脚本的新手,我正在尝试制作一个 ThirdPersonCamera,所以跟随 this tutorial 他可以上下左右正确移动鼠标

使用的脚本是

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;

由于新的输入系统,使用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;
}

得到了这个,但是如果我把鼠标放在一个轴上,它会一直旋转到那一边

那么...这是将旧的Input.GetAxis("Mouse X")Input.GetAxis("Mouse Y") 替换为新输入系统的正确方法吗?如何阻止它继续旋转?

【问题讨论】:

    标签: unity3d


    【解决方案1】:
    Mouse.current.delta.x.ReadValue()
    Mouse.current.delta.y.ReadValue()
    

    【讨论】:

      【解决方案2】:

      您可能尝试导入新的输入系统包以兼容多种输入设备。这些类型的错误是由于新旧输入系统包之间的冲突造成的,并且可能在最新更新中得到解决。要解决此问题,请转到 Edit -> Project Settings->Player->Under Other Settings 下的 Configuration 是 Active Input Handling 选项。选择两者。 Unity 将重新启动。现在你的问题应该解决了。您将能够同时使用旧的输入系统包和新的输入系统包。干杯。 PSClick here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-10-14
        • 1970-01-01
        • 2022-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多