【问题标题】:How Can I smooth out the Values of Mouse X in Unity如何在 Unity 中平滑鼠标 X 的值
【发布时间】:2022-01-25 17:03:27
【问题描述】:

我正在尝试创建一个脚本,使摄像头在 Z 轴动态上倾斜,这样当您向右看时,摄像头会像被拉扯一样向侧面倾斜,但不知何故,我的摄像头只是抖动,我知道为什么,但我不知道不知道如何修复命中。原因是鼠标 X 轴的值。当我打印出来时,我可以看到它的第二个值是 0,而其他值是真实值,所以我的相机从 0 捕捉到我想要的旋转。

lateUbdate函数中的第一部分仅用于cam的第一人称控制

    public float MausEmpfindlichkeit;
    float CamTilt;

    //transform
    public Transform PlayerBody;


    //floats
    float Xrot = 0f;


    //Scripte
    public RotaionSpeed PlayerSpeed;

    // Start is called before the first frame update
    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
    }

    // Update is called once per frame
    void LateUpdate()
    {
        TurnCamToSpeed();

        float MausX = Input.GetAxis("Mouse X") * Time.deltaTime * MausEmpfindlichkeit;
        float MausY = Input.GetAxis("Mouse Y") * Time.deltaTime * MausEmpfindlichkeit;

        Xrot -= MausY;

        transform.localRotation = Quaternion.Euler(Xrot, 0f, CamTilt);
        PlayerBody.Rotate(Vector3.up * MausX);

        print(Input.GetAxis("Mouse X"));

    }

    void TurnCamToSpeed()
    {
        float CamTiltValaue = Random.Range(0, Input.GetAxis("Mouse X"));
        CamTilt = -CamTiltValaue * 3;
        
    }

我的问题是,如何防止这种情况发生并获得鼠标 X 轴的平滑值

This is the current behaviour but it is hard so because of the low fps rate of the gif

【问题讨论】:

    标签: c# unity3d smoothing


    【解决方案1】:

    一般来说,GetAxisGetAxisRaw 相反已经平滑了一点。

    但不是硬设置

    CamTilt = -CamTiltValaue * 3;
    

    您可以使用 Mathf.Lerp 添加更多平滑,例如

    CamTilt = Mathf.Lerp(CamTilt, -CamTiltValaue * 3, someFactor * Time.deltaTime);
    

    其中someFactor 乘以Time.deltaTime 应该介于01 之间,所以你必须稍微考虑一下。

    但请注意,这可能会导致某些东西几乎不移动,因为您每帧都选择了一个新值,这可能最终或多或少地固定在中间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-18
      相关资源
      最近更新 更多