Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Gyroscope not work properly with some devices

Discussion in 'Scripting' started by unity_ZDYnbr1My1nHOg, Jan 12, 2020.

  1. unity_ZDYnbr1My1nHOg

    unity_ZDYnbr1My1nHOg

    Joined:
    Feb 16, 2019
    Posts:
    3
    Hi to everyone.
    I had write a script to rotate a rifle in my app and i have an issue on some devices. The input.gyro.attitude not work properly on my samsung s7 ( on stock rom , lineageos , and some other) and with lg g5. When the main character takes the rifle the rifle rotate but its like trembling.
    This is the script
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class RotateScript : MonoBehaviour
    7. {
    8.     private Gyroscope gyro;
    9.     float dx;
    10.     float dy;
    11.     float x;
    12.     float y;
    13.  
    14.     public string scenename;
    15.  
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {      
    19.         gyro = Input.gyro;
    20.          if(!gyro.enabled)
    21.         {
    22.              gyro.enabled = true;
    23.          }
    24.        
    25.          if (SystemInfo.supportsGyroscope)
    26.             {
    27.              //Gyro is available
    28.             }
    29.             else
    30.             {
    31.                 changemenuscene(scenename);
    32.             }
    33.      
    34.         x = Input.gyro.attitude.x;
    35.         y = Input.gyro.attitude.y;
    36.      
    37.     }
    38.  
    39.     // Update is called once per frame
    40.     void Update()
    41.     {
    42.         dx = Input.gyro.attitude.x - x;
    43.         dy = Input.gyro.attitude.y - y ;
    44.         gameObject.transform.Rotate(dy*8, dx*8, 0);
    45.      
    46.     }
    47.     public void changemenuscene( string scenename)
    48.     {
    49.     Application.LoadLevel (scenename);
    50.     }
    51. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    Not sure why the gyro wouldn't work on one specific device. Maybe there's a problem with the internal drivers on that device and it just needs to be rebooted?

    As far as as the trembling, looks like noisy gyro data. You can smooth it with a simple low pass filter, or else you can develop your own heuristic for deciding if a given sample is too far away from the previous one(s) and then ignore it.
     
  3. unity_ZDYnbr1My1nHOg

    unity_ZDYnbr1My1nHOg

    Joined:
    Feb 16, 2019
    Posts:
    3
    Thank you for your reply. I had reboot it many times. Your filter i think its good idea. Thank you for your advice. I will informe you if it work.
     
    Kurt-Dekker likes this.
  4. unity_ZDYnbr1My1nHOg

    unity_ZDYnbr1My1nHOg

    Joined:
    Feb 16, 2019
    Posts:
    3
    Hi again. Unfortunatly the filter did not solve the promplem. I tried again the app on different devices and works smoothly. The weird think is that when i run the app from the unity programe and use the galaxy s7 with unity remote works perfect. When i built the app and run it on the galaxy s7 and on the lg g5 i have this issue.