• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Shahid0090 · Aug 05, 2020 at 09:28 AM · accelerometergyro

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Shahid0090 · Aug 08, 2020 at 10:52 AM 0
Share

you are Great man thanks you saved my my time

avatar image
1

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Shahid0090 · Aug 05, 2020 at 11:13 AM 0
Share

ok thanks can you tell me how can i do this

avatar image davidcox70 Shahid0090 · Aug 05, 2020 at 11:20 AM 0
Share

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;



https://docs.unity3d.com/ScriptReference/Input-gyro.html

avatar image Shahid0090 davidcox70 · Aug 08, 2020 at 11:02 AM 0
Share

ok thanks man

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

133 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Help requested: Google VR / Phone rotation to gameobject. 1 Answer

Jet Accelerometer Script 1 Answer

How to get highest value for acceleration from Input.gyro 0 Answers

Desperate for help on control for a ball on android phone. 1 Answer

Detect car acceleration with device motion sensors and GPS 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges