2016-08-01 10 views
2

Ich mache eine Gyroskop-fähige App für Android/iOS, wo Sie Ihr Gyroskop verwenden können, um sich umzusehen. Ich möchte, dass der Player seine Kameraposition zurückstellt (die Szene vor dem Gerät), aber ich kann kein System dafür finden. HierUnity Gyroskop Kamera-Position zurücksetzen (wie oculus recenter Kamera)

ist der Code für umsah:

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 
      } 
     } 
    } 
} 

Antwort

0

Sie benötigen einen Ursprung Rotation hinzufügen - drehen Sie Ihre Drehung durch eine Quaternion, die die Umkehrung der Drehung ist, die Sie zurücksetzen möchten.

In Ihrem Fall wird dies sein:

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); 
      } 
     } 
    } 
} 
+0

nicht funktioniert, macht die Ansicht, alle seitwärts und verkorkste –

+0

Vielleicht haben Sie den Algorithmus falsch, oder was? –

+0

Mein Android-Gerät ist kaputt gegangen und kann dies momentan nicht beheben. Bitte versuchen Sie es in der Zwischenzeit: https://gist.github.com/chanibal/baf46307c4fee3c699d5 Sie können die Rotation umkehren (ändern Sie Zeile 23 in 'transform.localRotation = Quaternion.Inverse (Quaternion.Inverse (origin) * Input .gyro.attitude); ') –