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 /
  • Help Room /
avatar image
1
Question by thesanketkale · Feb 23, 2018 at 12:22 PM · quaternionrotate objectgyroscopecompassmagnetic

Rotate main camera using phone gyroscope with +Z-axis always aligned to North direction and +X-axis aligned to East direction

Hello fellow Unity Developers! I am facing a small issue with rotating main camera using system gyroscope. I am trying to fix the unity's XYZ such that +Z-axis is always aligned to North direction and +X-axis aligned to East direction. By fixing the unity's XYZ I mean that whenever I start the app by holding the phone in any direction or orientation, the reference 3D objects on +Z-axis and +X-axis should always be seen in the direction of true north and true east respectively.

I have come up with below code with a reference from this script . Now my code gives desired output when I start the app holding it up-right in landscape mode in *almost all orientations(*by almost all I mean, of all the orientations possible by placing the phone in a VR device). Only in case of 2 orientations viz, placing it flat on a table with the screen facing up and the screen facing the table, it behaves a bit randomly. This happens in an android phone(Xiomi Redmi Note 3) and an iOS phone(iPhone 5S) both, the special cases show reference objects in random directions when app started with phones placed flat with screen facing up or down.

I am using below code to rotate the camera transform:


 using UnityEngine;
 using System.Collections;
 
 public class GyroCameraManager : MonoBehaviour
 {
     // STATE
     private float _initialYAngle = 0f;
     private float _appliedGyroYAngle = 0f;
     private float _calibrationYAngle = 0f;
     private Transform _rawGyroRotation;
     private float _tempSmoothing;
 
     // SETTINGS
     [SerializeField] private float _smoothing = 0.1f;
 
     private IEnumerator Start()
     {
         Input.gyro.enabled = true;
         Input.compass.enabled = true;
         Application.targetFrameRate = 60;
 
         _rawGyroRotation = new GameObject("GyroRaw").transform;
         _rawGyroRotation.parent = transform;
         _rawGyroRotation.position = transform.position;
         _rawGyroRotation.rotation = transform.rotation;
 
         // Wait until gyro is active, then calibrate to reset starting rotation.
         yield return new WaitForSeconds(1);
 
         _initialYAngle = Input.compass.trueHeading;
         StartCoroutine(CalibrateYAngle());
     }
 
     private void Update()
     {
         ApplyGyroRotation();
         ApplyCalibration();
 
         transform.rotation = Quaternion.Slerp(transform.rotation, _rawGyroRotation.rotation, _smoothing);
     }
 
     private IEnumerator CalibrateYAngle()
     {
         _tempSmoothing = _smoothing;
         _smoothing = 1;
         _calibrationYAngle = _appliedGyroYAngle - _initialYAngle; // Offsets the y angle in case it wasn't 0 at edit time.
         yield return null;
         _smoothing = _tempSmoothing;
     }
 
     private void ApplyGyroRotation()
     {
         _rawGyroRotation.rotation = Input.gyro.attitude;
         _rawGyroRotation.Rotate(0f, 0f, 180f, Space.Self); // Swap "handedness" of quaternion from gyro.
         _rawGyroRotation.Rotate(90f, 180f, 0f, Space.World); // Rotate to make sense as a camera pointing out the back of your device.
         _appliedGyroYAngle = _rawGyroRotation.eulerAngles.y; // Save the angle around y axis for use in calibration.
     }
 
     private void ApplyCalibration()
     {
         _rawGyroRotation.Rotate(0f, -_calibrationYAngle, 0f, Space.World); // Rotates y angle back however much it deviated when calibrationYAngle was saved.
     }
 }


I am using unity 5.6.3p4. I do not have any other iOS device but I have tried in couple of android phones and the results are same. Please tell me what could be wrong with the code? Please help!

Comment
Add comment · Show 2
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 thesanketkale · Feb 27, 2018 at 09:34 AM 0
Share

Hello! Anyone here?

avatar image ihachani · Oct 29, 2018 at 09:36 AM 0
Share

Did you manage to solve this? I am facing a similar issue.

0 Replies

· Add your reply
  • Sort: 

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

129 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

Related Questions

Cant add Torque and Rotation to a rigidbody in the same frame? 2 Answers

Ask some problem about rotation, quaternion and angle 0 Answers

IOS gyro trick ? 0 Answers

Rotate an Object around selected local axis towards another object? 0 Answers

Gyro and North 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