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

Gyroscope Gimbal Lock

Discussion in 'Scripting' started by KevinFernandes, Oct 21, 2017.

  1. KevinFernandes

    KevinFernandes

    Joined:
    Jul 27, 2016
    Posts:
    2
    Okay guys i've tried pretty much all solutions I could find on the web and still couldn't fix this problem, also tried some scripts found on the web to no avail. I can't for the life of me figure this out

    I need a gyroscope script to get my camera to rotate on 360° on an android device but I'm having some huge gimbal lock issues. At a certain angle, the camera will glitch and do a weird rotation. This lasts for some frames and then the rotation is ok, but the glitch is highly disturbing... Can someone help me with this ?

    Heres my code :

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class GyroCamera : MonoBehaviour
    4. {
    5.     private bool gyroBool;
    6.     private Gyroscope gyro;
    7.     private Quaternion rotFix;
    8.     Quaternion quat;
    9.     Quaternion quatMap;
    10.  
    11.     public void Start()
    12.     {
    13.         Transform currentParent = transform.parent;
    14.         GameObject camParent = new GameObject("GyroCamParent");
    15.         camParent.transform.position = transform.position;
    16.         transform.parent = camParent.transform;
    17.  
    18.         gyroBool = SystemInfo.supportsGyroscope;
    19.  
    20.         if (gyroBool)
    21.         {
    22.             gyro = Input.gyro;
    23.             gyro.enabled = true;
    24.            
    25.             camParent.transform.eulerAngles = new Vector3(90, 180, 0);          
    26.             rotFix = new Quaternion(0, 0, 1, 0);
    27.         }
    28.     }
    29.  
    30.     public void Update()
    31.     {
    32.         if (gyroBool && this.gameObject.GetComponent<Camera>().enabled)
    33.         {
    34.             quatMap = gyro.attitude;
    35.             quat = gyro.attitude * rotFix;
    36.             transform.localRotation = quat;
    37.         }
    38.     }
    39. }
    I hope some good soul can help me on this... Thanks guys !
     
  2. KevinFernandes

    KevinFernandes

    Joined:
    Jul 27, 2016
    Posts:
    2
    No one has any idea how I could fix this issue, the help would be greatly needed ^^.
     
  3. GorasGames

    GorasGames

    Joined:
    May 1, 2014
    Posts:
    18
    Hi, I know this is an old topic but I'm having the same problem right now...and tried lot of solution for days but nothing working...Did you find any solution ?
     
  4. HazimMadbox

    HazimMadbox

    Joined:
    May 5, 2018
    Posts:
    1
    Hello

    Did you find a solution ? we faced the same problem