如何在当前设备方向启用陀螺仪相机 [英] How to enable gyroscope camera at current device orientation

查看:39
本文介绍了如何在当前设备方向启用陀螺仪相机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想启用陀螺仪控制的相机 onButtonClick 事件,但我希望它从相机的当前位置开始.目前,当陀螺仪被启用时,它会将相机移到一个新位置(可能是设备当前的陀螺仪旋转),而不是将其留在原处并从该点开始旋转.

I would like to enable gyro controlled camera onButtonClick event but I want it to start at the camera's current position. Currently when the gyro gets enabled it moves the camera off to a new position (probably the devices current gyro rotation) rather than leaving it where it is and gyro-ing from that point.

希望我说得有道理,但基本上我不希望用户注意到他们在游戏中看到的任何变化(即由陀螺仪控制的相机,但用户不会注意到这种变化).这是我正在使用的代码:

Hope I'm making sense, but basically I don't want the user to notice any change in what they are seeing in the game (ie. camera controlled by gyro but not that user would notice that change). Here's the code I'm using:

void Update () 
{
    Quaternion attitudeFix = new Quaternion (-gyro.attitude.x, -gyro.attitude.z, -gyro.attitude.y, gyro.attitude.w);
    Quaternion offsetRotation =  initialGyroRotation * attitudeFix;
    rotation = initialRotation * offsetRotation;
    transform.rotation = rotation;
}

public void EnableGyro() 
{
    initialGyroRotation = Input.gyro.attitude;
    initialRotation = transform.rotation;

    Debug.Log("initialRotation: " + initialRotation.ToString());
    Debug.Log("transform.rotation: " + transform.rotation.ToString());
    Debug.Log("initialGyroRotation: " + initialGyroRotation.ToString());
}

**

这是一个屏幕,显示了当用户将设备放在他们的脸(肖像)前面并向北走时,我希望视图的外观.无论应用启动时设备的方向如何,当手机以纵向方向向北行驶时(再次当用户通过手机看时),这就是它的外观.

Here's a screen of exactly how I want the view to look as the user is holding their device in front of their face (portrait) AND heading north. Regardless of the orientation of the device when the app starts, this is how it should look when heading north with phone in portrait orientation (again as the user is looking through the phone).

编辑 2:测试越来越混乱,所以我把代码放回了你的解决方案所建议的那样.还是有一点小问题,不过貌似这个脚本已经很接近了.主要问题是当我运行每个测试时,屏幕看起来不像上图,以奇怪的角度以设备启动应用程序.启动应用程序时设备的角度真的不重要,当指向北方和纵向时,它需要看起来像上面的屏幕.

EDIT 2: Tests were getting confusing so I put the code back to exactly how your solution suggests. There is still a slight problem, but it seems like this script is very close. The main problem is the screen doesn't look like the above pic when I run each test, starting the app with the device on strange angles. It really shouldn't matter what angle the device is when the app is started, it needs to look like the above screen when pointing north and portrait.

我需要做更多的测试,并且会用一个新的/干净的项目来做.

I need to do more tests, and will do so with a new/clean project.

推荐答案

您需要在AwakeStart 函数中获取偏移的相机位置.在 Update 函数中,将该偏移值应用于陀螺仪传感器的值.

You need to get the offset camera position in the Awake or Start function. In the Update function, apply that offset value to the value from the gyro sensor.

您似乎已经知道应该使用偏移量,但您没有正确使用.令人困惑的部分是获取需要从陀螺仪传感器值中减去当前相机旋转的偏移量.

It looks like you already know offset should be used but you are not doing that the right way. The confusing part is getting the offset which requires subtracting the current camera rotation from the gyro sensor value.

减去一个四元数,乘以Inverse 而不仅仅是Quaternion:

To subtract a Quaternion multiply the Inverse not just the Quaternion:

Quaternion = Quaternion *Quaternion.Inverse

要添加 Quaternion 乘以 Quaternion:

Quaternion = Quaternion * Quaternion.

<小时>

您的代码应该是这样的:


This is what your code should look like:

Quaternion offset;

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

void Start()
{
    //Subtract Quaternion
    offset = transform.rotation * Quaternion.Inverse(GyroToUnity(Input.gyro.attitude));
}

void Update()
{
    GyroModifyCamera();
}

void GyroModifyCamera()
{
    //Apply offset
    transform.rotation = offset * GyroToUnity(Input.gyro.attitude);
}

private static Quaternion GyroToUnity(Quaternion q)
{
    return new Quaternion(q.x, q.y, -q.z, -q.w);
}

这篇关于如何在当前设备方向启用陀螺仪相机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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