Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 TheCellCH · Jul 30, 2019 at 04:44 PM · rotationgyroscopegyro

Change Gyroscope forward

Hi I've asked on gamedev stack (https://gamedev.stackexchange.com/questions/174107/unity-gyroscope-orientation-attitude-wrong) but no answer so far, here is my question:

I am using the Unity reference and example implementation here https://docs.unity3d.com/ScriptReference/Gyroscope.html

I struggle to fix an orientation problem. When my phone lies flat on the ground, screen facing upwards, unity thinks I am looking as if I'd take a photo of a landscape. When I actually hold the phone as if I'd take a landscape photo, the screen shows me the sky. Rotating then shows me an arc from sky to my left or right side.

What I want is: Holding the phone as if I'd take a landscape picture should do the same in Unity.

Here is a short video where the phone lies flat and then I pick it up and look left and right on a chair.

alt text

and this is the codesnipped responsible:

     protected void Update()
     {
         GyroModifyCamera();
     }
     
     void GyroModifyCamera()
     {
         transform.rotation = GyroToUnity(Input.gyro.attitude);
     }
     
     // The Gyroscope is right-handed.  Unity is left handed.
     // Make the necessary change to the camera.
     private static Quaternion GyroToUnity(Quaternion q)
     {
         return new Quaternion(q.x, q.y, -q.z, -q.w);
     }


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

1 Reply

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

Answer by TheCellCH · Jul 30, 2019 at 06:46 PM

I solved the problem with another Quaternion multiplication (rotation). Keep in mind: The Quaternion multiplication order does matter.

alt text

The Code is the following

 private Gyroscope phoneGyro;
 private Quaternion correctionQuaternion;
 
 // Start is called before the first frame update
 void Start()
 {
     phoneGyro = Input.gyro;
     phoneGyro.enabled = true;
     correctionQuaternion = Quaternion.Euler(90f, 0f, 0f);
 }
 
 // Update is called once per frame
 void Update()
 {
         GyroModifyCamera();
 }
 
 // The Gyroscope is right-handed.  Unity is left handed.
 // Make the necessary change to the camera.
 void GyroModifyCamera()
 {
     Quaternion gyroQuaternion = GyroToUnity(Input.gyro.attitude);
     // rotate coordinate system 90 degrees. Correction Quaternion has to come first
     Quaternion calculatedRotation = correctionQuaternion * gyroQuaternion;
     transform.rotation = calculatedRotation;
 }
 
 private static Quaternion GyroToUnity(Quaternion q)
 {
     return new Quaternion(q.x, q.y, -q.z, -q.w);
 }


Comment
Add comment · Show 7 · 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 Petrit24 · Sep 09, 2019 at 02:16 PM 0
Share

You have provided great code, but how can I calibrate the gyro so that it does not matter in which direction you start with the phone it will set that rotation as the initial start rotation?

avatar image TheCellCH · Sep 09, 2019 at 02:45 PM 0
Share

From the top of my head: subtract the startup quaternion from the 90 degree quaternion and save it. For every calculation you multiply that in see line whith the comment about correction

avatar image Petrit24 TheCellCH · Sep 09, 2019 at 02:55 PM 0
Share

I don't really know what you mean exactly. Could you maybe explain it with code?

avatar image TheCellCH Petrit24 · Sep 09, 2019 at 03:21 PM 0
Share

Replace the correction quaternion withyoura calculated quaternion. Ins$$anonymous$$d of 90 degrees youll have to add more than 90 degrees based on the initial gyro position. But I'm just assu$$anonymous$$g, you'll need to try it out.

Show more comments
avatar image Marks4 · Jul 07, 2020 at 09:44 PM 0
Share

Can you please post the code to include how to set the initial rotation to where the virtual camera is looking in the editor?

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.

Follow this Question

Answers Answers and Comments

144 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Input.gyro.attitude not working on newer Android devices 3 Answers

3rd player orientation using gyro with offset 0 Answers

Android gyro 1 Answer

Clamp gyro rotation 0 Answers

3D camera relatively using gyro a la N.O.V.A. 2 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges