It is possible to rotate an object by importing the quaternion data from the smartphone's Gyro in Unity.
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.
How can I solve this?
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 = "1. playTime : " + time.ToString("N2")
+ "\r\n\n2. Gyroattitude : " + Input.gyro.attitude
+ "\r\n\n3. Gyroattitude.ToEuler : " + Input.gyro.attitude.ToEuler()
+ "\r\n\n4. Gyroattitude.ToEulerAngles : " + Input.gyro.attitude.ToEulerAngles()
+ "\r\n\n5. Gyroattitude.eulerAngles : " + Input.gyro.attitude.eulerAngles
+ "\r\n\n6. phone.transform.rotation : " + phone.transform.rotation
+ "\r\n\n7. phone.transform.eulerAngles : " + phone.transform.eulerAngles;
}
}
Site 1: https://flowtree.tistory.com/28
I downloaded Unity Remote from the google play store and set up various settings on my cell phone by referring to Site 1.
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.
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.
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.
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.
UnityRemote operated both on the LG V40 and Samsung S20+.