Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Gyro + Compass Camera Rotation

Discussion in 'iOS and tvOS' started by mimminito, Feb 11, 2016.

  1. mimminito

    mimminito

    Joined:
    Feb 10, 2010
    Posts:
    780
    Hi,

    Im struggling to get a script working which uses both the Gyro and the Compass for rotation. Its going to be used for AR GPS locations. Below is the code I am currently working with (most of it is from another thread on here for Gyro rotation). The Gyro rotation is great, but I need the device to accurately rotate based on the heading of the compass as well, so I can look at various POI's accurately. Right now the POI's are in difference locations depending on the direction by device is facing when the app starts.

    Any help would be greatly appreciated!

    Code (CSharp):
    1. // Gyroscope-controlled camera for iPhone  Android revised 2.26.12
    2. // Perry Hoberman <hoberman@bway.net>
    3. //
    4. // Usage:
    5. // Attach this script to main camera.
    6. // Note: Unity Remote does not currently support gyroscope.
    7. //
    8. // This script uses three techniques to get the correct orientation out of the gyroscope attitude:
    9. // 1. creates a parent transform (camParent) and rotates it with eulerAngles
    10. // 2. for Android (Samsung Galaxy Nexus) only: remaps gyro.Attitude quaternion values from xyzw to wxyz (quatMap)
    11. // 3. multiplies attitude quaternion by quaternion quatMult
    12. // Also creates a grandparent (camGrandparent) which can be rotated with localEulerAngles.y
    13. // This node allows an arbitrary heading to be added to the gyroscope reading
    14. // so that the virtual camera can be facing any direction in the scene, no matter what the phone's heading
    15. //
    16. // Ported to C# by Simon McCorkindale <simon <at> aroha.mobi>
    17. using UnityEngine;
    18.  
    19. namespace OMobile
    20. {
    21.     public class GyroCam : MonoBehaviour
    22.     {
    23.  
    24.         #region Private Variables
    25.  
    26.         private bool gyroBool;
    27.         private Gyroscope gyro;
    28.         private Quaternion rotFix;
    29.         private Transform mGyroCamParentTrans;
    30.         private Transform mGyroCamGrandParentTrans;
    31.  
    32.         #endregion
    33.  
    34.         #region Unity Methods
    35.  
    36.         void Start ()
    37.         {
    38.             Transform currentParent = transform.parent;
    39.             GameObject camParent = new GameObject ("GyroCamParent");
    40.             mGyroCamParentTrans = camParent.transform;
    41.             camParent.transform.position = transform.position;
    42.             transform.parent = camParent.transform;
    43.             GameObject camGrandparent = new GameObject ("GyroCamGrandParent");
    44.             mGyroCamGrandParentTrans = camGrandparent.transform;
    45.             camGrandparent.transform.position = transform.position;
    46.             camParent.transform.parent = camGrandparent.transform;
    47.             camGrandparent.transform.parent = currentParent;
    48.        
    49.             #if UNITY_3_4
    50.             gyroBool = Input.isGyroAvailable;
    51.             #else
    52.             gyroBool = SystemInfo.supportsGyroscope;
    53.             #endif
    54.             gyroBool = true;
    55.        
    56.             if (gyroBool) {
    57.                 gyro = Input.gyro;
    58.                 gyro.enabled = true;
    59.                 Input.compass.enabled = true;
    60.  
    61.                 SetupRotations ();
    62.             } else {
    63.                 #if UNITY_EDITOR
    64.                 print ("NO GYRO");
    65.                 #endif
    66.             }
    67.         }
    68.  
    69.         void Update ()
    70.         {
    71.             if (gyroBool == gameObject.activeInHierarchy) {
    72.                 Quaternion quatMap = Quaternion.identity;
    73.                 #if UNITY_IPHONE
    74.                 quatMap = gyro.attitude;
    75.                 #elif UNITY_ANDROID
    76.                 quatMap = new Quaternion(gyro.attitude.x,gyro.attitude.y,gyro.attitude.z,gyro.attitude.w);
    77.                 #endif
    78.  
    79.                 UpdateCompassRotation ();
    80.  
    81.                 transform.localRotation = quatMap * rotFix;
    82.             }
    83.         }
    84.  
    85.         #endregion
    86.  
    87.         #region Public Methods
    88.  
    89.         public void ResetRotations ()
    90.         {
    91.             mGyroCamParentTrans.localRotation = Quaternion.identity;
    92.             mGyroCamGrandParentTrans.localRotation = Quaternion.identity;
    93.             transform.localRotation = Quaternion.identity;
    94.         }
    95.  
    96.         public void SetupRotations ()
    97.         {
    98.             if (!gyroBool) {
    99.                 return;
    100.             }
    101.             if (Screen.orientation == ScreenOrientation.LandscapeLeft) {
    102.                 mGyroCamParentTrans.transform.eulerAngles = new Vector3 (90, 180, 0);
    103.             } else if (Screen.orientation == ScreenOrientation.Portrait) {
    104.                 mGyroCamParentTrans.transform.eulerAngles = new Vector3 (90, 180, 0);
    105.             } else if (Screen.orientation == ScreenOrientation.PortraitUpsideDown) {
    106.                 mGyroCamParentTrans.transform.eulerAngles = new Vector3 (90, 180, 0);
    107.             } else if (Screen.orientation == ScreenOrientation.LandscapeRight) {
    108.                 mGyroCamParentTrans.transform.eulerAngles = new Vector3 (90, 180, 0);
    109.             } else {
    110.                 mGyroCamParentTrans.transform.eulerAngles = new Vector3 (90, 180, 0);
    111.             }
    112.        
    113.             if (Screen.orientation == ScreenOrientation.LandscapeLeft) {
    114.                 rotFix = new Quaternion (0, 0, 1, 0);
    115.             } else if (Screen.orientation == ScreenOrientation.Portrait) {
    116.                 rotFix = new Quaternion (0, 0, 1, 0);
    117.             } else if (Screen.orientation == ScreenOrientation.PortraitUpsideDown) {
    118.                 rotFix = new Quaternion (0, 0, 1, 0);
    119.             } else if (Screen.orientation == ScreenOrientation.LandscapeRight) {
    120.                 rotFix = new Quaternion (0, 0, 1, 0);
    121.             } else {
    122.                 rotFix = new Quaternion (0, 0, 1, 0);
    123.             }
    124.  
    125.             //mGyroCamGrandParentTrans.localRotation = Quaternion.Euler (new Vector3 (0, Input.compass.magneticHeading, 0));
    126.         }
    127.  
    128.         #endregion
    129.  
    130.         #region Private Methods
    131.  
    132.         private void UpdateCompassRotation ()
    133.         {
    134.             Vector3 rot = mGyroCamGrandParentTrans.localEulerAngles;
    135.             rot.y = Input.compass.magneticHeading;
    136.             mGyroCamGrandParentTrans.localRotation = Quaternion.Lerp (mGyroCamGrandParentTrans.localRotation, Quaternion.Euler (rot), Time.deltaTime * 8f);
    137.         }
    138.  
    139.         #endregion
    140.     }
    141. }
     
  2. mimminito

    mimminito

    Joined:
    Feb 10, 2010
    Posts:
    780
    Bump, can anyone help here?
     
  3. camilosw

    camilosw

    Joined:
    Jun 9, 2013
    Posts:
    9
  4. wilkens

    wilkens

    Joined:
    Feb 3, 2014
    Posts:
    6
    Man F*** Unity forum. It has been 8 years but still nobody answered the question.
     
  5. Edisyo

    Edisyo

    Joined:
    Dec 11, 2017
    Posts:
    9
    2018 - 2016 = 2 years. Do you know what is math? :p
     
    BroPet and chantey like this.