繁体   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