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
0
Question by Aruarian · Apr 28, 2017 at 05:54 AM · cameracamera rotatecamera-lookgyroscope

How to set viewpoint regardless of tilt of device when use gyroscope

I succeeded in using the gyroscope to allow the camera viewpoint to change the angle according to the tilt of the device.

But what I want to do is, let camera see where I want to regardless of device tilt when the game starts.

I have already tried one but failed.

A) Quaternion value of the device

B) Quaternion value of the device when game scene starts

C) difference in Euler Angle between A and B

D) Quaternion value of main camera looking at where I want to be

I assigned a quaternion of D plus C to the angle of the camera.

Test results were smooth, but when device had a certain angle, the view of the camera rotated to an unusual angle.

I find it seems to be a problem caused by Gimbal Lock and I have not found a way to solve it.

Here is my code.

Look Update

         private bool gyroEnabled;
         private Gyroscope gyro;
         private GameObject cameraContainer;
         public Quaternion rot;
         
         public Quaternion finalQuaternion;
         public float finalEulerX = 0;
         public float finalEulerY = 0;
         public float finalEulerZ = 0;
         
         public Quaternion quaternion;
         public float QuaternionToEulerX = 0f;
         public float QuaternionToEulerY = 0f;
         public float QuaternionToEulerZ = 0f;
         
         public bool initialQuaternionCheck = false;
         public Quaternion initialQuaternion;
         public float initialQuaternionToEulerX = 0f;
         public float initialQuaternionToEulerY = 0f;
         public float initialQuaternionToEulerZ = 0f;
         
         public float alteredEulerX = 0f;
         public float alteredEulerY = 0f;
         public float alteredEulerZ = 0f;
         
         public bool initialLookAtQuaternionCheck = false;
         public Quaternion lookAtQuaternion;
         public float lookAtQuaternionToEulerX = 0f;
         public float lookAtQuaternionToEulerY = 0f;
         public float lookAtQuaternionToEulerZ = 0f;
 
 
         void Start() {
             cameraContainer = new GameObject("Cameara Container");
             cameraContainer.transform.position = transform.position;
             transform.SetParent(cameraContainer.transform);
             gyroEnabled = EnableGyro();
         }
 
         private bool EnableGyro()
         {
             if (SystemInfo.supportsGyroscope)
             {
                 gyro = Input.gyro;
                 gyro.enabled = true;
 
                 cameraContainer.transform.rotation = Quaternion.Euler(90f, 90f, 0f);
                 rot = new Quaternion(0, 0, 1, 0);
 
                 return true;
             }
             return false;
         }
 
 
         private void SetInitialQuaternion()
         {
             initialQuaternionCheck = true;
 
             initialQuaternion = quaternion;
 
             initialQuaternionToEulerX = initialQuaternion.eulerAngles.x;
             initialQuaternionToEulerY = initialQuaternion.eulerAngles.y;
             initialQuaternionToEulerZ = initialQuaternion.eulerAngles.z;
         }
 
 
         private void SetInitialLookAtQuaternion()
         {
             initialLookAtQuaternionCheck = true;
 
             transform.LookAt(focus.transform);
 
             lookAtQuaternion = transform.localRotation;
 
             lookAtQuaternionToEulerX = lookAtQuaternion.eulerAngles.x;
             lookAtQuaternionToEulerY = lookAtQuaternion.eulerAngles.y;
             lookAtQuaternionToEulerZ = lookAtQuaternion.eulerAngles.z;
         }
 
 
         private void CheckAlteredQuaternion()
         {
             alteredEulerX = QuaternionToEulerX - initialQuaternionToEulerX;
             alteredEulerY = QuaternionToEulerY - initialQuaternionToEulerY;
             alteredEulerZ = QuaternionToEulerZ - initialQuaternionToEulerZ;
         }
 
 
         void Update()
         {
             if (gyroEnabled)
             {
                 quaternion = gyro.attitude * rot; // A) Quaternion value of the device
                 QuaternionToEulerX = quaternion.eulerAngles.x;
                 QuaternionToEulerY = quaternion.eulerAngles.y;
                 QuaternionToEulerZ = quaternion.eulerAngles.z;
                 
                 if (initialQuaternionCheck == false)
                 {
                     SetInitialQuaternion(); // B) Quaternion value of the device when game scene starts
                 }
 
                 CheckAlteredQuaternion(); // C) difference in Euler Angle between A and B
 
                 if (initialLookAtQuaternionCheck == false)
                 {
                     SetInitialLookAtQuaternion(); // D) Quaternion value of main camera looking at where I want to be
                 }
 
                 Vector3 result = lookAtQuaternion.eulerAngles - new Vector3(alteredEulerX, alteredEulerY, alteredEulerZ);
 
                 finalQuaternion = Quaternion.Euler(result); 
 
                 transform.localRotation = finalQuaternion;
             }
         }

Do i miss something?

or Wrong approach?

If not, how can I change my camera angle freely from the Gimbal Lock?

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

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

128 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

Related Questions

viewing on the Mouse Y axis not working. 0 Answers

How to recreate the Silent Hill 1 camera style? 0 Answers

Camera Quaternion.RotateTowards without rolling/banking 1 Answer

(Solveed) How to make object face same direction as its parent? 0 Answers

Help me create camere collision for mobile 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