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 Trstek · Jul 23, 2016 at 06:38 AM · rotationquaternionlookatslerp

Mobile Gyroscope, make Camera always rotating towards zero point using Quaternion

So basically like this gif looks like https://giphy.com/gifs/l0MYrjDwyvzLmj7Ms

^ left box represents gyroscope, right box represents player camera

here is the working code with eulerAngles,

 gyroRotation = Input.gyro.attitude;
 gyroRotation = Vector3.SlerpUnclamped(previousGyroscopeRotation, gyroRotation, fDamping * Time.deltaTime);
 deltaRotation = (previousGyroscopeRotation - gyroRotation);

 finalRotation = finalRotation - deltaRotation;

 if (finalRotation.x != 0) 
     finalRotation.x = Mathf.LerpUnclamped(finalRotation.x, 0, 1 * Time.deltaTime);

 if (finalRotation.y != 0)
     finalRotation.y = Mathf.LerpUnclamped(finalRotation.y, 0, 1 * Time.deltaTime);

 if (finalRotation.z != 0)
     finalRotation.z = Mathf.LerpUnclamped(finalRotation.z, 0, 1 * Time.deltaTime);

 transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(finalRotation), Time.deltaTime);

 previousGyroscopeRotation = gyroRotation;


(but because of angle problems (ie: jumping from 350˙ to 10´ instead of 370)) internet suggested i use Quaternions, but have no idea how to achieve the same effect

 transform.rotation = Quaternion.Slerp(transform.rotation, Input.gyro.attitude, Time.deltaTime);
 transform.rotation = Quaternion.Slerp(transform.rotation, zeroPosition, Time.deltaTime);

this is the peak of my skills with Quaternions, if someone could shed some light on how to achieve this as simply as possible

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

2 Replies

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

Answer by Arshia001 · Jul 23, 2016 at 04:25 PM

To solve your Euler angle problems, you can use Mathf.LerpAngle which takes the jump from 360 to 0 into account. Also, the equivalent of (0,0,0) for rotation is Quaternion.identity, since quaternions can never be zero. I don't think using angles is a bad idea, if you know what you're doing. Simply changing Lerp to LerpAngle should probably fix any problems you have.

Comment
Add comment · 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
0

Answer by Trstek · Jul 24, 2016 at 02:54 PM

Ok So I've used Mathf.LerpAngle, it did fix the angle problems I've had, but there were still the usual EulerAngle problems, where rotation x over ~75˙, y and z axis both flip to 180, and that again screwed the system up.

So I've research Quaternions, and basically few lines of code does exactly what I wanted with no problems :)

 gyroRotQuat = Input.gyro.attitude;
 deltaRotQuat = (previousGyroscopeRotQuat * (Quaternion.Inverse(gyroRotQuat)));
 finalRotQuat = finalRotQuat * (Quaternion.Inverse(deltaRotQuat));
 transform.rotation = Quaternion.Slerp(transform.rotation, deltaRotQuat, Time.deltaTime);
 previousGyroscopeRotQuat = gyroRotQuat;



Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Delayed LookAt, but only on one axis 1 Answer

How to use Quaternion.slerp and Quaternion.LookRotation with a child gameobject 0 Answers

Object rotation naturally change 0 Answers

[Solved] Smooth rotation with keydown 2 Answers

Smooth look at 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