Unity-陀螺仪-仅绕一轴旋转 [英] Unity - Gyroscope - Rotation Around One Axis Only

查看:352
本文介绍了Unity-陀螺仪-仅绕一轴旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Unity制作使用陀螺仪的游戏.我正在尝试检测设备围绕穿过屏幕的轴的旋转,我假设它是Unity Space中的Z轴.我尝试使用Input.gyro.attitude.eulerAngles.z,但是在使设备绕其他轴倾斜时不会保持恒定,例如,我希望它保持恒定,以便仅由"Z轴"确定汽车方向盘的旋转.我该如何实施?

I'm using Unity to make a game that uses the Gyroscope. I am trying to detect the rotation of the device around the axis that goes through the screen, I'm assuming it's the Z-axis in Unity Space. I tried using Input.gyro.attitude.eulerAngles.z but it doesn't stay constant when tilting the device around the other axes, I want it to stay constant so that only the "Z-axis" determines the rotation of a car's steering wheel, for example. How can I implement this?

我尝试做其他事情.我用Input.gyro.rotationRateUnbiased代替了Input.gyro.attitude.eulerAngles

I tried doing something else. I used Input.gyro.rotationRateUnbiased instead of Input.gyro.attitude.eulerAngles

代码如下:

    Vector3 gInput = Input.gyro.rotationRateUnbiased;
    gyroEuler += gInput * Time.deltaTime * Mathf.Rad2Deg;

    float rollAngle = gyroEuler.z;

    while (rollAngle < 0)
    {
        rollAngle += 360;
    }
    while (rollAngle > 360)
    {
        rollAngle -= 360;
    }

但是gyroEuler将被设置为(0, 0, 0)而不管游戏开始时的设备方向,因此在开始时就不会知道设备方向,并且Input.gyro.rotationRateUnbiased也非常不准确.我想像许多游戏一样旋转屏幕,例如 Temple Run 2 ,但是我希望能够将屏幕旋转360度并获得旋转角度.

But gyroEuler will be set to (0, 0, 0) regardless of the devices orientation at the start of the game so the device orientation will not be known in the beginning and also Input.gyro.rotationRateUnbiased is very inaccurate. I want to rotate the screen the way many games, like Temple Run 2, do but I want to be able to rotate it 360 degrees and get that angle of rotation.

推荐答案

好吧,我刚刚尝试了一下,效果很好:

Well I just tried it and it works well :

private void Start()
{
    Input.gyro.enabled = true;
}

private void Update()
{
    Vector3 previousEulerAngles = transform.eulerAngles;
    Vector3 gyroInput = -Input.gyro.rotationRateUnbiased;

    Vector3 targetEulerAngles = previousEulerAngles + gyroInput * Time.deltaTime * Mathf.Rad2Deg;
    targetEulerAngles.x = 0.0f; // Only this line has been added
    targetEulerAngles.z = 0.0f;

    transform.eulerAngles = targetEulerAngles;

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

您需要做的是创建一个中央 Transform (或使用您的播放器),并将您的相机作为其子级.然后设置相机的z距离,高度和角度以调整视图.最后,将上述脚本附加到中央的 Transform (变换):旋转该脚本将使您的 Camera (摄像机)沿弧线移动.

What you need to do is create a central Transform (or use your player) and parent your Camera as child of it. Then set the Camera z-distance, height and angle to adjust your view. Finally attach the above script to the central Transform: rotating it will make your Camera move around following an arc.

编辑:如果您想使用Input.gyro.attitude,则可以按照以下步骤操作

If you want to use Input.gyro.attitude instead you can do it as follow

private Vector3 startEulerAngles;
private Vector3 startGyroAttitudeToEuler;

private void Start()
{
    Input.gyro.enabled = true;
    startEulerAngles = transform.eulerAngles;
    startGyroAttitudeToEuler = Input.gyro.attitude.eulerAngles;
}

private void Update()
{
    Vector3 deltaEulerAngles = Input.gyro.attitude.eulerAngles - startGyroAttitudeToEuler;
    deltaEulerAngles.x = 0.0f;
    deltaEulerAngles.z = 0.0f;

    transform.eulerAngles = startEulerAngles - deltaEulerAngles;
}

public void ResetGyro()
{
    startGyroAttitudeToEuler = Input.gyro.attitude.eulerAngles;
}

我想您正在寻找的是类似

I guess what you are looking for will be something like this: you can then check upVec.y and upVec.x for your Down/Up and Left/Right axis.

首先创建一个空的 GameObject ,将其用作包含陀螺仪旋转的虚拟对象,并将其设置在脚本的phoneDummy字段中.

First create an empty GameObject that will be used as a dummy containing gyroscope rotation and set it in phoneDummy field of the script.

public Transform phoneDummy;

private void Start()
{
    Input.gyro.enabled = true;
}

private void Update()
{
    Vector3 gyroEuler = Input.gyro.attitude.eulerAngles;
    phoneDummy.transform.eulerAngles = new Vector3(-1.0f * gyroEuler.x, -1.0f * gyroEuler.y, gyroEuler.z);

    Vector3 upVec = phoneDummy.transform.InverseTransformDirection(-1f * Vector3.forward);
}

upVec.y将是您的 螺距 ,而upVec.x将是您的 滚动 轴. (请在此处以获得轴参考)

upVec.y will be your pitch and upVec.x will be your roll axis. (check here for axis reference)

但是请记住,Input.gyro中的值并不是完美无缺的:如果您要查找更持久的值(当然,但不那么精确的值),则可能要使用Input.compass.magneticHeading.

But keep in mind values from Input.gyro are far from flawless: you may want to use Input.compass.magneticHeading if you are looking for a more persistent value (but of course less precise one).

希望这会有所帮助,

这篇关于Unity-陀螺仪-仅绕一轴旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆