2016-08-01 3 views
2

Я делаю приложение с поддержкой гироскопа для Android и ios, где вы можете использовать свой гироскоп, чтобы осмотреться. Я бы хотел, чтобы плеер сбросил свою позицию камеры (сцену ретранслятора Infront устройства), но я не могу заставить систему работать для этого.Unity Gyroscope Сброс положения камеры (например, камера заднего обзора oculus)

Вот код для озираясь:

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

ответ

0

Вы должны добавить вращение происхождения - повернуть вращение на кватернион, который является обратным вращением, который вы хотите сбросить.

В вашем случае это будет:

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

Не работает, делает вид сбоку и все перепутались –

+0

Может быть, вы получили алгоритм неправильно или что-то? –

+0

Мое устройство Android сломалось и не может отлаживать это на данный момент. Попробуйте это время: https://gist.github.com/chanibal/baf46307c4fee3c699d5 Возможно, вы захотите инвертировать поворот (смените строку 23 на 'transform.localRotation = Quaternion.Inverse (Quaternion.Inverse (origin) * Input .gyro.attitude); ') –

Смежные вопросы