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

Gyrometer always returns 0 vectors in Unity 5

Discussion in 'Scripting' started by TheWulo, Mar 5, 2015.

  1. TheWulo

    TheWulo

    Joined:
    Nov 16, 2014
    Posts:
    5
    Hi,

    I'm trying to use Gyrometer in my Unity 5 Project, but after so many hours of digging I cannot come up with a solution.

    The thing is, the same code works fine on Unity 4.6, but not on 5.0. It looks like this:

    Code (CSharp):
    1.  void Start()
    2.     {
    3.         originalPos = gameObject.transform.position;
    4.         GyroscopeEnabled = SystemInfo.supportsGyroscope;
    5.         if (GyroscopeEnabled)
    6.         {
    7.             gyro = Input.gyro;
    8.             gyro.enabled = true;
    9.         }
    10.     }
    11.  
    12.     void FixedUpdate()
    13.     {
    14.         if (GyroscopeEnabled)
    15.         {
    16.             xTilt = Input.acceleration.x;
    17.             yTilt = -Input.acceleration.y;
    18.            
    19.             //This does not work either
    20.             //xTilt = gyro.attitude.x;
    21.             //yTilt = gyro.attitude.y;
    22.  
    23.             //This works. I was just checking if the problem is for sure in gyro
    24.             //xTilt = Mathf.Sin(Time.time);
    25.             //yTilt = Mathf.Cos(Time.time);
    26.  
    27.             targetPos = new Vector3(originalPos.x + xTilt * sensitivity, originalPos.y + yTilt * sensitivity, originalPos.z);
    28.  
    29.             gameObject.transform.position = Vector3.Lerp(originalPos, targetPos, smoothSpeed);
    30.  
    31.         }
    32.     }
    33.  
    34.     void OnGUI()
    35.     {
    36.         if (GyroscopeEnabled)
    37.         {
    38.  
    39.                 GUI.Label(new Rect(10, Screen.height / 2 - 100, 500, 100), "gyro supported " + Time.time);
    40.                 GUI.Label(new Rect(10, Screen.height / 2, 500, 100), "rotation rate:" + gyro.rotationRate.ToString());
    41.                 GUI.Label(new Rect(10, Screen.height / 2 + 50, 500, 100), "gravity:      " + gyro.gravity.ToString());
    42.                 GUI.Label(new Rect(10, Screen.height / 2 + 100, 500, 100), "attitude:     " + gyro.attitude.ToString());
    43.                 GUI.Label(new Rect(10, Screen.height / 2 + 150, 500, 100), "type:         " + gyro.GetType().ToString());
    44.                 GUI.Label(new Rect(10, Screen.height / 2 + 200, 500, 100), "xTil:         " + xTilt.ToString());
    45.                 GUI.Label(new Rect(10, Screen.height / 2 + 250, 500, 100), "yTil:         " + yTilt.ToString());
    46.         }
    47.         else
    48.         {
    49.             GUI.Label(new Rect(10, Screen.height / 2 - 100, 500, 100), "gyro not supported");
    50.         }
    51.     }
    On app builded with 5.0, all displayed values are 0, no matter what I do with my smartphone.
    On 4.6 works fine and shows correct values, so it's not a falut of gyroscope itself.
    I calibrated gyroscope in my android device before trying to use it, worked fine.

    I was looking into PlayerSetting>Other Setting, for iOS i have Accelerometer Frequency and i made sure is it NOT set on Disabled, but for Android there is no such an option.

    I also tryied to add "android.hardware.sensor.accelerometer" to AndroidManifest, but it was not a suprice that it didn't help.

    Did you run across similar problem or maybe have an idea what could I do?

    Can you check if this code works for you in Unity 5?

    Thank you in advance for answers

    Sincerly,
    Matt
     
  2. soybenito

    soybenito

    Joined:
    Jul 9, 2012
    Posts:
    5
    I have the same problem, did you solve it by any chance ?
     
  3. TheWulo

    TheWulo

    Joined:
    Nov 16, 2014
    Posts:
    5
    I didn't, just gave up on idea.

    I think it is the problem of some plugin which is overriding values of gyro, so not really Unity fault. Gyro works find on new "empty" project.

    I was searching for the code which does this inside of plugin, but I could not find it.

    If you will find the answer post it here please.

    Have a good day.
     
  4. ZYSHAQ

    ZYSHAQ

    Joined:
    Jun 26, 2015
    Posts:
    76
    Same problem folks
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    There have been several major versions of 5.x since this thread was posted. Are you on the latest?