【问题标题】:Gyroscope with compass help needed需要指南针帮助的陀螺仪
【发布时间】:2017-10-26 00:09:51
【问题描述】:

我需要一个游戏对象指向北方,并且我想将它与 gyro.attitude 输入相结合。我曾尝试过一步完成,但未成功。也就是说,我无法制作任何我在网上找到的陀螺仪脚本,以满足始终指向北方的附加要求。相信我,我已经尝试了我能找到的关于这个主题的所有脚本。我推断这是不可能的,并且认为可以做到可能是愚蠢的;至少不是这样(即多合一)。我想你可以说我推测你不能同时做两件事。然后我想也许我可以通过分解职责来达到同样的效果。也就是说,一个始终通过 Y 轴指向北方的游戏对象。太好了,就这样完成了:

_parentDummyRotationObject.transform.rotation = Quaternion.Slerp(_parentDummyRotationObject.transform.rotation, Quaternion.Euler(0, 360 - Input.compass.trueHeading, 0), Time.deltaTime * 5f);

当游戏对象在 Y 轴上指向北方时,我想添加第二个游戏对象,在本例中为相机,在 X 轴和 Z 轴上使用陀螺仪输入进行旋转。我必须消除相机上的 Y 轴的原因是因为我得到了双重旋转。两个物体同时旋转(即相机和游戏对象),180 度旋转在场景中产生 360 度。请记住,我需要游戏对象始终根据设备指南针指向北方 (IRL)。如果我的设备指向东方,那么我的游戏对象将在统一场景中旋转 90 度,因为它指向(旋转)北方。

我已经阅读了很多关于陀螺仪相机控制器的信息,我看到很多提到的一件事是你不应该尝试仅在 1 或 2 轴上执行此操作(限制它),使用四元数时,如果你不这样做是不可能的知道你在做什么,我显然不知道。

我已经尝试了这个已解决问题的所有 3 个解决方案:Unity - Gyroscope - Rotation Around One Axis Only 并且每个解决方案都未能在 1 轴上旋转我的相机以满足我的旋转需求。想我会尝试让 1 轴工作,然后再用第 2 轴搅浑水。顺便说一句,我的要求很简单,就是相机只能基于我设备的 X 轴在 1 个轴(任何方向)上旋转。如果我能解决 X,那么我认为让 Z 陀螺仪输入也能控制相机会很棒。到目前为止,我无法仅在 1 个轴 (X) 上控制相机。无论如何,这是我的发现...

使用 Input.gyro.rotationRateUnbiased 的第一个解决方案完全不准确。也就是说,如果我将我的设备旋转了几次,然后将我的手机/设备放在我的桌子上,那么相机每次都会处于不同的旋转/位置。没有一致性。这是我第一次尝试/解决方案的代码:

<code>
private void Update()
{
  Vector3 previousEulerAngles = transform.eulerAngles;
  Vector3 gyroInput = Input.gyro.rotationRateUnbiased;
  Vector3 targetEulerAngles = previousEulerAngles + gyroInput * Time.deltaTime * Mathf.Rad2Deg;
  targetEulerAngles.y = 0.0f; 
  targetEulerAngles.z = 0.0f;
  transform.eulerAngles = targetEulerAngles;
}
</code>

第二种解决方案非常一致,因为我可以旋转我的设备,然后将其放在桌子上,统一摄像头始终处于相同的位置/旋转/状态。我遇到的问题是相机会在一个轴上旋转(在这种情况下是 X),但是当我在 y 或 x 轴上旋转我的设备时它会这样做。我手机的任何一种旋转/移动都会导致统一相机在 X 上移动。我不明白为什么手机的 y 旋转会导致相机在 X 上旋转。这是我的解决方案 #2 的代码:

    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.y = 0.0f;
  deltaEulerAngles.z = 0.0f;
  transform.eulerAngles = startEulerAngles - deltaEulerAngles;
}

第三个解决方案:我不确定如何完成最后一个解决方案,所以它从来没有真正起作用。 2轴归零时,相机只是从左到右和后翻转,或者从上到下和后翻转;取决于注释掉的轴。如果没有一个轴被注释掉(如原始解决方案),相机将在所有轴上旋转。这是我尝试 #3 的代码:

    private void Start()
{
  _upVec = Vector3.zero;
  Input.gyro.enabled = true;
  startEulerAngles = transform.eulerAngles;
}
private void Update()
{
  Vector3 gyroEuler = Input.gyro.attitude.eulerAngles;
  phoneDummy.transform.eulerAngles = new Vector3(-1.0f * gyroEuler.x, -1.0f * gyroEuler.y, gyroEuler.z);
  _upVec = phoneDummy.transform.InverseTransformDirection(-1f * Vector3.forward);
  _upVec.z = 0;
//    _upVec.x = 0;
  _upVec.y = 0;
  transform.LookAt(_upVec);
//    transform.eulerAngles = _upVec;
}

本来我以为这是我的技能,但是在花了一个月的时间之后,我开始认为这是不可能的。但那是不可能的。我知道要吸收很多东西,但这是一个非常简单的概念。

有什么想法吗?

编辑:我想我会添加我的层次结构:

CameraRotator(带脚本的父级)-> MainCamera(子级)

CompassRotator(父级)-> Compass(带有旋转父级的脚本的子级)

【问题讨论】:

  • 我得到了其中的一些,但没有其他的。 1.你想让一个游戏对象面朝北。 2.你想把这个和gyro.attitude输入结合起来……你为什么要做#2?这是 2D 还是 3D 对象?
  • 这是一个 3D 增强现实应用程序,所以我使用设备摄像头和陀螺仪为 Unity 场景/对象添加“生命”。我拥有的当前版本有一个在 Y 轴上旋转的 3d 指南针。其他 2 个轴为 0,因此指南针不会像我想要的那样在场景中“浮动”。它非常僵硬,因为它只是在 y 上旋转(以保持北方方向/指向)。谢谢
  • this 帖子。 HeadTrack 示例可能会为您提供所需的内容。
  • 感谢程序员,这实际上是一个我以前没见过的例子;考虑到我做了多少搜索,这令人震惊。只是去给你看。会让你知道...

标签: c# unity3d quaternions gyroscope


【解决方案1】:

我将通过以下方式执行此操作: Camara 默认 0, 0, 0 旋转 Screenshot

对象放置在相机默认位置的中心。

相机脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
    Camera m_MainCamera;
    // Start is called before the first frame update
    void Start()
    {
        // Disable the sleep timeout during gameplay. 
        // You can re-enable the timeout when menu screens are displayed as necessary. 
        Screen.sleepTimeout = SleepTimeout.NeverSleep;
        // Enable the gyroscope. 
        if (SystemInfo.supportsGyroscope)
        {
            Input.gyro.enabled = true;
        }
        m_MainCamera = Camera.main;
        m_MainCamera.enabled = true;
    }

    // Update is called once per frame
    void Update()
    {
        if (m_MainCamera.enabled)
        {
            // First - Grab the Gyro's orientation. 
            Quaternion tAttitude = Input.gyro.attitude;
            // The Device uses a 'left-hand' orientation, we need to transform it to 'right-hand'
            Quaternion tGyro = new Quaternion(tAttitude.x, tAttitude.y, -tAttitude.z, -tAttitude.w);

            // the gyro attitude is tilted towards the floor and upside-down reletive to what we want in unity.  
            // First Rotate the orientation up 90deg on the X Axis, then 180Deg on the Z to flip it right-side up. 
            Quaternion tRotation = Quaternion.Euler(-90f, 0, 0) * tGyro;
            tRotation = Quaternion.Euler(0, 0, 180f) * tRotation;

            // You can now apply this rotation to any unity camera!
            m_MainCamera.transform.localRotation = tRotation;
        }
    }
}

有了这个脚本,无论如何我的对象总是面向南方。

如果您希望对象面向 NORTH,您只需将视图在 Y 轴上旋转 180º 作为最后一次旋转:

Quaternion tRotation = Quaternion.Euler(-90f, 0, 0) * tGyro;
tRotation = Quaternion.Euler(0, 0, 180f) * tRotation;
//Face NORTH:
tRotation = Quaternion.Euler(0,180f, 0) * tRotation;

希望这可能会有所帮助;)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多