我假设它是Unity Space中的Z轴 . 我尝试使用 Input.gyro.attitude.eulerAngles.z
,但它没有't stay constant when tilting the device around the other axes, I want it to stay constant so that only the 481397 determines the rotation of a car'方向盘,例如 . 我该如何实现呢?
EDIT: 我尝试过做别的事 . 我使用了 Input.gyro.rotationRateUnbiased
而不是 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度并获得旋转角度 .
1 回答
好吧,我刚试了一下,效果很好:
您需要做的是创建一个中心 Transform (或使用您的播放器)并将您的 Camera 作为其子级 . 然后设置 Camera z距离,高度和角度以调整视图 . 最后将上面的脚本附加到中央 Transform :旋转它将使你的 Camera 在弧后移动 .
EDIT: 如果您想使用
Input.gyro.attitude
,您可以按照以下方式进行操作EDIT 2:
我想你要找的是this:你可以检查
upVec.y
和upVec.x
为你的向下/向上和向左/向右轴 .首先创建一个空的 GameObject ,它将用作包含陀螺仪旋转的虚拟对象,并将其设置在脚本的
phoneDummy
字段中 .upVec.y
将是你的 pitch 而upVec.x
将是你的 roll 轴 . (检查here以获取轴参考)但要记住
Input.gyro
中的值远非完美:如果您正在寻找更持久的值(但当然不那么精确),您可能想要使用Input.compass.magneticHeading
.希望这可以帮助,