簡體   English   中英

如何使用移動陀螺儀輸入旋轉相機時鎖定相機z的旋轉

[英]How to lock rotation in z of my camera while rotating the camera by using mobile gyroscope input

我要使用陀螺儀輸入來統一旋轉攝像機,因為我希望播放器環顧x,y。 但是,z也會以某種方式旋轉。 如何保持z旋轉為0,同時至少保持x和y的平滑旋轉?

一直在搜尋,但尚未找到任何可將z保持為0的解決方案。

這是我用於旋轉的代碼,它附在相機上。

 private void Start()
{
    _rb = GetComponent<Rigidbody>();
    Input.gyro.enabled = true;
}

 private void Update()
{
    float gyro_In_x = -Input.gyro.rotationRateUnbiased.x;
    float gyro_In_y = -Input.gyro.rotationRateUnbiased.y;

    transform.Rotate(gyro_In_x, gyro_In_y, 0);
}

我的猜測是問題來自於transform.rotation *= Quaternion.Euler(gyro_In_x, gyro_In_y, 0); 線。 嘗試設置旋轉值而不是乘以:

private void Start()
{
    _rb = GetComponent<Rigidbody>();
    Input.gyro.enabled = true;
}

private void Update()
{
    Vector3 previousEulerAngles = transform.eulerAngles;
    Vector3 gyroInput = -Input.gyro.rotationRateUnbiased; //Not sure about the minus symbol (untested)

    Vector3 targetEulerAngles = previousEulerAngles + gyroInput * Time.deltaTime * Mathf.Rad2Deg;
    targetEulerAngles.z = 0.0f;

    transform.eulerAngles = targetEulerAngles;

    //You should also be able do it in one line if you want:
    //transform.eulerAngles = new Vector3(transform.eulerAngles.x - Input.gyro.rotationRateUnbiased.x * Time.deltaTime * Mathf.Rad2Deg, transform.eulerAngles.y - Input.gyro.rotationRateUnbiased.y * Time.deltaTime * Mathf.Rad2Deg, 0.0f);
}

希望這可以幫助,

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM