Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 boddole · Oct 11, 2014 at 04:48 AM · c#quaternionmath

Issue with Adding Quaternions

Hello everyone, I've managed to cause a value error when I attempt to add two Quaternions. This issue then creates a rotation problem when I feed that new Quaternion to a Slerp method.

In Detail:

Essentially, I'm getting values for a Vector3, making sure that the Z value is 0, then convert the Vector3 to a Quaternion. Then I multiply this new Quaternion with the current object's rotation to get a new additive rotation target for the Slerp method.

But for some reason, when I debug the value of the current object rotation multiplied by the target Quaternion, the Z value is not 0. This non-zero Z value then throws of the rotation of the object because it gets fed into a Slerp method (that now has the wrong values).

So:

targetPosition Z value is 0.

targetQuat Z value (as an euler angle) is 0.

But tempQuat (as an euler angle) has a non-0 Z value (and the object has no Z rotation of its own).

Any ideas as to why this is are greatly appreciated. Thank for reading.

Code Below:

     public void MoveCamera()
     {
         targetPosition = analogInput.AnalogeStickMovementPercentage();
         targetPosition.z = 0f;
         Debug.Log ("targetPosition is: " + targetPosition); 
 
         targetQuat = Quaternion.Euler (targetPosition);
         Debug.Log ("targetQuat.eulerangles is: " + targetQuat.eulerAngles);
 
         Quaternion tempQuat = transform.rotation * targetQuat;
         Debug.Log ("tempQuat.eulerangles is: " + tempQuat.eulerAngles);
 
         transform.rotation = Quaternion.Slerp (transform.rotation, transform.rotation * targetQuat, cameraMovementSpeed * Time.deltaTime);
     }

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 robertbu · Oct 11, 2014 at 05:01 AM

The euler angles reported by Unity is derived from the Quaternion. There are multiple euler representations for any given physical representation. For example, if you did the following:

  transform.eulerAngles = Vector3(180, 0, 0);
  Debug.Log(transform.eulerAngles);

The Debug.Log() output is (0,180,180)...the same physical rotation represented differently. So the fact that 'z' is not zero is only an issue if at some other point in your code you are reading the 'z' from eulerAngles. As long as you treat eulerAngles as write-only, and don't run into a gimble lock situation, things should work.

I guess what I'm saying is that, unless you are reading eulerAngles at some other point in the code, look beyond the non-zero z value for your problem.

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 boddole · Oct 11, 2014 at 05:58 AM 0
Share

Will do, but any other ideas why a Slerp would start tilting on the Z axis? When I run this script, the camera eventually rotates completely sideways...

alt text

camera tilt.png (19.0 kB)
avatar image robertbu · Oct 11, 2014 at 07:57 AM 0
Share

Your behavior is not unexpected. Quaternion rotations always take the shortest distance, and if that means some 'z' rotation, then that is what happens.

Consider punting on the Quaternions, and working directly with eulerAngles. $$anonymous$$aintain your own Vector3 that represents the current rotation. $$anonymous$$ake changes to that Vector3, then assign it to transform.eulerAngles. Be sure to never read from eulerAngles.

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

28 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

Related Questions

Comparing lerping quaternion 1 Answer

Finding Distance between Angles and Points 2 Answers

Trying to find boolean value in another script 0 Answers

How To Add PlayerPrefs Scores? 1 Answer

How to Name Individual Buttons Within a List 2 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