title: Get Gyro 3-Axis Angle of Smartphone in Unity
body: <p>It is possible to rotate an object by importing the quaternion data from the smartphone's Gyro in Unity.</p>
  <p>The current problem is that in the process of converting from quaternion to Euler to know the 3-axis angle, a gimbal lock occurs and the data on the x-axis does not come out properly.</p>
  <p>How can I solve this?</p>
  <p>Code</p>
  <pre><code>using UnityEngine;
  
  public class Device_Data_Load : MonoBehaviour
  {
  
  public GameObject phone;
  
  public float time;
  
  void Start()
  {
      Input.gyro.enabled = true;
  }
  
  void Update()
  {
      time += Time.deltaTime;
      phone.transform.rotation = Input.gyro.attitude;
  
      text.text = &quot;1. playTime : &quot; + time.ToString(&quot;N2&quot;)
                + &quot;\r\n\n2. Gyroattitude : &quot; + Input.gyro.attitude
                + &quot;\r\n\n3. Gyroattitude.ToEuler : &quot; + Input.gyro.attitude.ToEuler()
                + &quot;\r\n\n4. Gyroattitude.ToEulerAngles : &quot; + Input.gyro.attitude.ToEulerAngles()
                + &quot;\r\n\n5. Gyroattitude.eulerAngles : &quot; + Input.gyro.attitude.eulerAngles
                + &quot;\r\n\n6. phone.transform.rotation : &quot; + phone.transform.rotation
                + &quot;\r\n\n7. phone.transform.eulerAngles : &quot; + phone.transform.eulerAngles;
  }
  </code></pre>
  <p>}</p>
  <p>Site 1: <a href="https://flowtree.tistory.com/28" rel="nofollow noreferrer">https://flowtree.tistory.com/28</a></p>
  <p>I downloaded Unity Remote from the google play store and set up various settings on my cell phone by referring to Site 1.</p>
  <p>In the Unity project, we created one empty game object and added a text object in the center of the game scene. If you add and execute two objects from the Inspector, the following results will be obtained.</p>
  <p>The rotation is Quaternion, so it does not take gimbal lock, but it seems to take gimbal lock the moment you use Quaternion.eulerAngles in the process of converting it to an angle that you can recognize.</p>
  <p>For reference, Input.gyro.attitude is a Quaternion data type.
  Therefore, the data on the x-axis does not come out between 90 and 270.</p>
  <p>Another solution would be to get the GameObject.rotation value and avoid gimbal lock when converting to Euler. However, the method of importing values ​​from the Inspector to the Unity editor cannot be used because it must be resolved at build time.</p>
  <p>UnityRemote operated both on the LG V40 and Samsung S20+.</p>