【问题标题】:Unity Gyroscope Reset Camera Position(like oculus recenter camera)Unity Gyroscope 重置相机位置(如 oculus 重新定位相机)
【发布时间】:2016-08-01 04:51:38
【问题描述】:

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

这是环顾四周的代码:

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's parent to that
            GameObject camParent = new GameObject ("CamParent");
            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 ("Recenter View")) {
                //RECENTER THE CAMERA VIEW
            }
        }
    }
}

【问题讨论】:

    标签: android ios unity3d camera gyroscope


    【解决方案1】:

    您需要添加一个原点旋转 - 将您的旋转旋转一个与您要重置的旋转相反的四元数。

    在你的情况下,这将是:

    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's parent to that
                GameObject camParent = new GameObject ("CamParent");
                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 ("Recenter View")) {
                    //RECENTER THE CAMERA VIEW
                    origin = Quaternion.Inverse(transform.localRotation);
                }
            }
        }
    }
    

    【讨论】:

    • 不工作,使视图全部横向并混乱
    • 我的安卓设备坏了,目前无法调试。同时请尝试以下操作:gist.github.com/chanibal/baf46307c4fee3c699d5 您可能想要反转旋转(将第 23 行更改为transform.localRotation=Quaternion.Inverse(Quaternion.Inverse(origin)*Input.gyro.attitude);)
    • 我之前在github上试过那个脚本,根本不行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-29
    • 1970-01-01
    • 1970-01-01
    • 2012-06-02
    • 1970-01-01
    • 1970-01-01
    • 2014-03-20
    相关资源
    最近更新 更多