- Home /
how can i get exact my mobile device physical rotation on UI text.
Hi i am making the Bubble level app using unity and i want to show the XYZ angles of my device in degrees. currently i am using Input.acceleration to achieve this but its not providing the accurate values for angle. help me to solve this problem or suggest me another way to make my bubble level app . thanks in advance
Answer by Llama_w_2Ls · Aug 05, 2020 at 04:23 PM
private bool gyroEnabled;
private UnityEngine.Gyroscope gyro;
private GameObject GyroControl;
private Quaternion rot;
public Text text;
Transform t;
public bool Xmode = true;
public bool Zmode = false;
float Xangle;
float Zangle;
public bool Switch = false;
private void Start()
{
Screen.sleepTimeout = SleepTimeout.NeverSleep;
GyroControl = new GameObject("Gyro Control");
GyroControl.transform.position = new Vector3(transform.position.x, 1, transform.position.z);
transform.SetParent(GyroControl.transform); //parents the object to an empty control object
gyroEnabled = EnableGyro();
t = transform;
}
private bool EnableGyro()
{
if (SystemInfo.supportsGyroscope)
{
gyro = Input.gyro;
gyro.enabled = true;
GyroControl.transform.rotation = Quaternion.Euler(90f, -90f, 0f); //These offset values are essential for the gyroscope to orientate itself correctly
rot = new Quaternion(0, 0, 1, 0);
return true;
}
return false;
}
private void Update()
{
if (gyroEnabled == true && Xmode == true)
{
transform.localRotation = gyro.attitude * rot;
t.eulerAngles = new Vector3(t.eulerAngles.x, 0, 0);
if (t.eulerAngles.x >= 180)
{
Xangle = 270 - t.eulerAngles.x;
}
else
{
Xangle = 90 - t.eulerAngles.x;
}
DisplayXangles();
}
else if (gyroEnabled == true && Zmode == true)
{
transform.localRotation = gyro.attitude * rot;
t.eulerAngles = new Vector3(0, 0, t.eulerAngles.z);
if (t.eulerAngles.z >= 180)
{
Zangle = 270 - t.eulerAngles.z;
}
else
{
Zangle = 90 - t.eulerAngles.z;
}
DisplayZangles();
}
text.transform.rotation = Quaternion.Euler(0, 0, -t.eulerAngles.z);
}
public void DisplayXangles()
{
if (Switch == false)
{
text.text = Mathf.Abs(Xangle).ToString("0") + "°";
}
else if (Switch == true)
{
if (90 + Xangle <= 90)
text.text = Mathf.Abs(90 + Xangle).ToString("0") + "°";
else
text.text = Mathf.Abs(Xangle - 90).ToString("0") + "°";
}
}
public void DisplayZangles()
{
if (Switch == false)
{
text.text = Mathf.Abs(Zangle).ToString("0") + "°";
}
else if (Switch == true)
{
if (90 + Zangle <= 90)
text.text = Mathf.Abs(90 + Zangle).ToString("0") + "°";
else
text.text = Mathf.Abs(Zangle - 90).ToString("0") + "°";
}
}
public void SetXmodeToTrue()
{
Xmode = true;
Zmode = false;
}
public void SetZmodeToTrue()
{
Zmode = true;
Xmode = false;
}
public void SwitchWhenPressed()
{
if (Switch == false)
Switch = true;
else
Switch = false;
}
So this is how I created a bubble level (or an inclinometer to be more precise). The last 2 functions: SetXModeToTrue(), SetZModeToTrue() are called on button presses, which switches from measuring the angle of your phone on the x to the angle on your phone on the z. SwitchWhenPressed() is another function called on button press, which basically offsets your rotation. E.g., if perfectly flat was 90 degrees, you could press a button called Switch and it would say 0 degrees instead. Hope this helps anyone who needs it!
Answer by davidcox70 · Aug 05, 2020 at 09:43 AM
I believe you can get it from Input.gyro.attitude. This will return a Quaternion which you can convert to Eulers if you need an XYZ interpretation.
I haven't tried it, but according to the docs, this should give you the XYZ angles of your device;
Quaternion deviceRotation = Input.gyro.attitude;
Vector3 XYZangles = deviceRotation.eulerAngles;
Your answer
Follow this Question
Related Questions
detect 360 spin on mobile device 1 Answer
How to create a camera that is controlled by a mobile device's in built sensors? 1 Answer
How do I access the Accelerometer and Gyroscope on my Surface Pro? 2 Answers
Help requested: Google VR / Phone rotation to gameobject. 1 Answer
Jet Accelerometer Script 1 Answer