Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在使用移动陀螺仪输入旋转相机时锁定相机的z轴旋转_C#_Unity3d_Mobile - Fatal编程技术网

C# 如何在使用移动陀螺仪输入旋转相机时锁定相机的z轴旋转

C# 如何在使用移动陀螺仪输入旋转相机时锁定相机的z轴旋转,c#,unity3d,mobile,C#,Unity3d,Mobile,我使用陀螺仪输入旋转相机,因为 我想让玩家看看x,y。 然而,不知何故,在z轴上也有旋转。 如何保持z旋转为0,同时至少保持x和y方向的平滑旋转 一直在谷歌上搜索,但还没有找到任何解决方案来真正保持 z在0 这是我用来旋转的代码,它附在相机上 private void Start() { _rb = GetComponent<Rigidbody>(); Input.gyro.enabled = true; } private void Update() {

我使用陀螺仪输入旋转相机,因为 我想让玩家看看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*=四元数.Eulergyro_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);
}

希望这有帮助,

抱歉,我刚刚注意到我在更新中使用了错误的代码。减号基于我在本教程中的介绍:我认为使用旋转也可能会在Z轴上引入不必要的旋转,这可能是错误的:我的解决方案操纵Transform.euleranges直接解决了您的问题吗?是的,谢谢!我用这段代码来代替它,它也做同样的事情,但对我来说要短得多,更清晰得多,以防有人想知道。浮动陀螺仪_In_x=-Input.gyro.rotationRateUnbiased.x;浮动陀螺仪输入y=-输入.gyro.rotationRateUnbiased.y;transform.eulerAngles+=新矢量3陀螺输入x,陀螺输入y,0.0f;很高兴它有帮助!请记住,编程时,预先制作的工具很有用,但您必须了解在这里使用它们时意味着什么旋转方法:这就是为什么我总是喜欢使用我确信的东西,如手动编辑Transform.euleranges,但不确定给定方法的工作方式: