菜鸟教程小白 发表于 2022-12-11 17:08:13

android - Unity Gyroscope 重置相机位置(如 oculus 重新定位相机)


                                            <p><p>我正在为 android/ios 制作一个支持陀螺仪的应用程序,您可以在其中使用您的陀螺仪环顾四周。我想让玩家重置他们的相机位置(在设备前面重新居中场景),但我无法让系统为此工作。</p>

<p>这是环顾四周的代码:</p>

<pre><code>using UnityEngine;
using System.Collections;

public class CameraControl : MonoBehaviour {
    void Start () {
      if (SystemInfo.supportsGyroscope) {
            Input.gyro.enabled = true;

            //Create parent object and set this object&#39;s parent to that
            GameObject camParent = new GameObject (&#34;CamParent&#34;);
            camParent.transform.position = transform.position;
            transform.parent = camParent.transform;

            // Rotate the parent object by 90 degrees around the x axis
            camParent.transform.Rotate (Vector3.right, 90);
      }
    }

    void Update () {
      if (SystemInfo.supportsGyroscope) {
            Quaternion rotation = new Quaternion (Input.gyro.attitude.x, Input.gyro.attitude.y, -Input.gyro.attitude.z, -Input.gyro.attitude.w);
            transform.localRotation = rotation;
      }
    }

    void OnGUI () {
      if (SystemInfo.supportsGyroscope) {
            GUILayout.Label (transform.localRotation.ToString());
            GUILayout.Label (transform.parent.rotation.ToString());

            if (GUILayout.Button (&#34;Recenter View&#34;)) {
                //RECENTER THE CAMERA VIEW
            }
      }
    }
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您需要添加一个原点旋转 - 将您的旋转旋转一个与您要重置的旋转相反的四元数。</p>

<p>在你的情况下,这将是:</p>

<pre><code>using UnityEngine;
using System.Collections;

public class CameraControl : MonoBehaviour {
    void Start () {
      if (SystemInfo.supportsGyroscope) {
            Input.gyro.enabled = true;

            //Create parent object and set this object&#39;s parent to that
            GameObject camParent = new GameObject (&#34;CamParent&#34;);
            camParent.transform.position = transform.position;
            transform.parent = camParent.transform;

            // Rotate the parent object by 90 degrees around the x axis
            camParent.transform.Rotate (Vector3.right, 90);
      }
    }

    Quaternion origin = Quaternion.identity;

    void Update () {
      if (SystemInfo.supportsGyroscope) {
            Quaternion rotation = new Quaternion (Input.gyro.attitude.x, Input.gyro.attitude.y, -Input.gyro.attitude.z, -Input.gyro.attitude.w);
            transform.localRotation = rotation * origin;
      }
    }

    void OnGUI () {
      if (SystemInfo.supportsGyroscope) {
            GUILayout.Label (transform.localRotation.ToString());
            GUILayout.Label (transform.parent.rotation.ToString());

            if (GUILayout.Button (&#34;Recenter View&#34;)) {
                //RECENTER THE CAMERA VIEW
                origin = Quaternion.Inverse(transform.localRotation);
            }
      }
    }
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于android - Unity Gyroscope 重置相机位置(如 oculus 重新定位相机),我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/38691228/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/38691228/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: android - Unity Gyroscope 重置相机位置(如 oculus 重新定位相机)