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 doomprodigy · May 12, 2016 at 11:48 AM · gyroscopequaternionstiltcompass

Gyroscope Magnetic North.

Hi there, I asked a question recently but I believe it was a little confusing and went un answered. I believe I understand the situation a little bit better now and am unsure on how to do it. The gyroscopes attitude is based on the direction of a compass I am guessing So north is north etc. What I need to do I think, is somehow make the gyroscopes attitude relative to the magnetic north (North in relation to the device). So that when I tilt in relation to it, no matter where the Person/Humans rotation is in the world my light will rotate in the right direction. Here's a link to the previous thread and I've posted my code below. Thank you :) http://answers.unity3d.com/questions/1182107/gyroscope-tilting-rotation.html#comment-1182146

  private Gyroscope gyro;
      public Quaternion rotation = Quaternion.identity;
      private Quaternion initialGyroRotation;
      private Quaternion initialRotation;
      private Quaternion baseRotation;
  
      void Start ()
      {
          gyro = Input.gyro;
          gyro.enabled = true;
          initialRotation = transform.rotation;
          baseRotation = transform.rotation;
          initialGyroRotation = Quaternion.identity;
          transform.rotation = gyro.attitude;
          Screen.orientation = ScreenOrientation.Portrait;
      }
  
      void Update ()
      {
          #if UNITY_IOS
          Quaternion attitudeFix = new Quaternion (gyro.attitude.x, gyro.attitude.y, gyro.attitude.z, gyro.attitude.w);
          #endif
          #if UNITY_ANDROID
          Quaternion attitudeFix = new Quaternion (gyro.attitude.x, gyro.attitude.y, -gyro.attitude.z, -gyro.attitude.w);
          #endif
          Quaternion offsetRotation = Quaternion.Inverse (initialGyroRotation) * attitudeFix;
          rotation = initialRotation * offsetRotation;
          transform.rotation = rotation;
  }
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

Answer by elenzil · May 12, 2016 at 06:44 PM

hm, there may be some confusion here.

the gyroscope in devices like a smartphone has nothing to do with north or magnetic fields. the gyroscope reports the angular velocity the device is rotating with. eg, if your device is still, the gyroscope reports 0 deg/s for all 3 axes. if your device is rotating around say the X axis, then the gyroscope's X axis should indicate that.

for getting the device's orientation relative to the earth's magnetic field, you want to access the magnetometer. In unity, this has been nicely packaged up in a class called the Compass.

Comment
Add comment · Show 2 · 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 doomprodigy · May 17, 2016 at 11:14 AM 0
Share

I guess I got a little confused you see. I'm after just the pitch and roll of the phone. To tilt a light. But lets say I rotated the phone 90 degrees or upside down or walk around my house. The gyroscopes pitch and roll change. I figured there might be a way to make it always relative to the north of the phone to solve the problem.

avatar image elenzil doomprodigy · May 17, 2016 at 04:31 PM 0
Share

ah. if you want the pitch and roll of the phone, you may want the Accelerometer.

phones have three motion/orientation detecting components: Accelerometer measures linear acceleration, including gravity. This can be used to deter$$anonymous$$e pitch and roll, but cannot be used to deter$$anonymous$$e yaw. Gyroscope measures angular velocity, which is the change in roll/pitch/yaw over time. but it cannot tell you the absolute roll/pitch/yaw. $$anonymous$$agnetometer measures the phone's orientation relative to the earth's magnetic field.

depending on what you need, you might be able to use just the accelerometer. there are many articles in google about these.

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

43 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

Related Questions

Fixed tilt rotation on speed up problem 0 Answers

unity horizontal and vertical axis based on gyro? 2 Answers

What is Input.gyro.attitude exactly? 0 Answers

Unity 3d Phone Direction? 0 Answers

Re-calibarting Compass 1 Answer


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