Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 massfar2017 · Feb 22, 2019 at 05:31 PM · camera rotategyroscope360-degrees

How can I make the camera rotate with gyroscope appropriately?

I want the camera to rotate 360º degrees using the gyroscope. I have this code:

 void Awake() {
         Input.gyro.enabled = true;
     }
 
 void Update() {
         transform.rotation = Input.gyro.attitude;
         transform.Rotate(0f, 0f, 180f, Space.Self);
         transform.Rotate(90f, 180f, 0f, Space.World);
     }

Which works fine if I don't care about the camera having a starting rotation. But I actually want the camera to start looking with the rotation associated with the camera preview. I want it to start looking at something, and then start gyroscoping from there. So I changed the code a bit to this:

    void Awake() {
         Input.gyro.enabled = true;
     }
 
    void Start() {
         offset = transform.rotation * Quaternion.Inverse(GyroToUnity(Input.gyro.attitude));
     }
 
   void Update() {
      transform.rotation = offset * GyroToUnity(Input.gyro.attitude);
   }

   private static Quaternion GyroToUnity(Quaternion q) {
     return new Quaternion(q.x, q.y, -q.z, -q.w);
   }

Now it starts looking at the right place, but when I start moving the phone around the camera rotation gets funky, it starts turning upside down. I don't know what to do, I searched everywhere. I tried this and this. Thank you for your time.

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
0

Answer by massfar2017 · Mar 01, 2019 at 11:35 AM

BUMP BUMP

Comment
Add comment · Show 4 · 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 xxmariofer · Mar 01, 2019 at 12:04 PM 0
Share

Hello, i once used indoor atlas that uses this code for solving that issue: (all credit to them)

     Quaternion rot = Quaternion.Inverse(new Quaternion(orientation.x, orientation.y, -orientation.z, orientation.w));
     Camera.main.transform.rotation = Quaternion.Euler(new Vector3(90.0f, 0.0f, 0.0f)) * rot;

orientation is the quaternion retrieve from the gyroscope. the previous link is a link to a demo project provided by Indoor Atlas that you can try since maybe their localization implementation might work even better for you :)

avatar image hellaandrew xxmariofer · Feb 20, 2021 at 10:05 PM 0
Share

This snippet helped me out! Thanks

avatar image sath · Mar 01, 2019 at 02:04 PM 0
Share

https://gist.github.com/kormyen/a1e3c144a30fc26393f14f09989f03e1

avatar image SWISH93 sath · Apr 05 at 09:13 AM 0
Share

Hey sorry I am a noob, in order to use the script attached on the github, I simply need to Add Component > New Script to my scene active camera, correct?

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

104 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

Related Questions

Starting position gyroscope camera unity 0 Answers

Rotating a camera using the gyroscope 4 Answers

Shaking screen using the gyroscope attitude 1 Answer

Calibration Script 0 Answers

How do I prevent camera shaking when focused on gameobject? 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