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 Efril · Oct 10, 2016 at 11:49 AM · rotationbugquaternions

Quaternion.FromToRotation - is it a bug? Possible workarounds?

Hello all. I just faced a very strange behavior when calculating rotations using Quaternion class. FromToRotation gives me different results with the same input depending on where it was called. Code of the method below:

 public virtual bool TryHorizontalAimTo(Vector3 Target)
     {
         Target.y = Ship.transform.position.y;
         
         Vector3 targetDirection = Vector3.Normalize(Target - Ship.transform.position);
         Quaternion rotation = Quaternion.FromToRotation(_actualForwardDirection.normalized, targetDirection);
 
         if (Ship.name == "PrototypeShip_2" && name == "TurretB")
         {
             Debug.Log("Target dir: " + targetDirection);
             Debug.Log("Forward dir: " + _actualForwardDirection);
             Debug.Log("Rotation: " + rotation.eulerAngles);
         }
 
         return TryApplyRotation(rotation);
     }

If called from Start() and from FixedUpdate() this method produces the following logs: alt text

If called from LateUpdate() it gives me the expected result of rotation around Y axis: alt text

As you can see in both cases I have (0.7, 0, 0.7) and (-0.7, 0, -0.7) vectors but rotations are different for some reason. I have Rigidbody attached to the GameObject but it is IsKinematic in both cases so this should not be a problem.

How should I handle with this? Are any workarounds exist?

Thanks in advance.

UPDATE: I tested it more and it seems no matter when the method is called. It looks like the issue appears only when object rotated at 45 degrees or 225 degrees which leads to (0.7, 0, 0.7) and (-0.7, 0, -0.7) vector parameters passed to FromToRotation. With other angles it wors fine.

start.png (22.1 kB)
lateupdate.png (24.8 kB)
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 Sergio7888 · Oct 10, 2016 at 02:32 PM 0
Share

LateUpdate is only called after all updates are executed, the result is different because all other transformation scripts already executed, in Update some scripts are to be executed.

avatar image Efril Sergio7888 · Oct 10, 2016 at 02:48 PM 0
Share

It looks like no difference method was called from Start or from LateUpdate. I updated the question.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by flaviusxvii · Oct 10, 2016 at 03:48 PM

A couple of things. Your "from" and "to" look to be very nearly colinear.. exact opposite directions. There's an infinite number of ways to rotate from one to the other. It's the pathological undefined case for rotating with quaternions. The only case where you wouldn't know for sure which way it's going to go..

Now.. as to the reason you're getting different results. When you print a Vector3 to the console, you only get to see 1 decimal of precision. If you were to print the components with more precision you'd see that they're probably slightly different (floats are notorious for rounding and precision errors accumulating as math is done to them). It's those little differences that are going to tip the Quaternion math to choosing one rotation over another.

Comment
Add comment · Show 5 · 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 Efril · Oct 10, 2016 at 05:24 PM 0
Share

You are right about getting different results - I displayed floats in the log with 4 decimal places and they are sligtly different. That is why I updated my question. FromToRotation between 45 Y degrees and 225 Y degrees gives not 180 degrees around Y as I expected but 270 around Y and 180 around Z. If it is normal 'mathematical' behaviour of quaternions than it is quite strange as to me.

avatar image flaviusxvii Efril · Oct 10, 2016 at 06:57 PM 0
Share

not 180 degrees around Y as I expected

Your expectation is the problem here. Quaternions don't prefer rotating around any particular axis.

avatar image Owen-Reynolds · Oct 10, 2016 at 05:27 PM 0
Share

The other trick is that eulerAngles aren't the real rotation - it's just the best way to describe the real quat. I think (0,270,180) is a rotation around a southeast arrow, so an "over the top." A way to test is to run it slowly, using Slerp. Or maybe just print angleAxis(180,southeast).

If you do the math, (0,270,180) is correct: 270 on Y faces west, with the original arrow pointing southeast. Then a 180 spin on Z (which is now facing west) flips the southeast arrow into a northeast arrow. But, again, that's only a clumsy euler description, and not the real rotation.

avatar image Efril Owen-Reynolds · Oct 10, 2016 at 07:09 PM 0
Share

I applied Quaternion to my GameObject not eulerAngles. In the image below you can see that back 'turret' rotated inside the 'hull' and also you can see 'guns' protruding from the hull (red square) which means turret also rotated ins$$anonymous$$d of seeing behind the 'ship'. It is not that I expected.alt text

turretmissing.png (371.3 kB)
avatar image flaviusxvii Efril · Oct 10, 2016 at 08:15 PM 0
Share

Turrets are very common. Try this:

 Quaternion currentOrientation = transform.rotation;
 Quaternion toTargetOrientation = Quaterion.LookRotation(targetDirection, Vector3.up);

 Quaternion rotation = Quaternion.RotateTowards(currentOrientation, toTargetOrientation, 360f);

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

73 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

Related Questions

Smooth rotate between changing rotations (already using Lerp, problems arise) 0 Answers

Weapon Spray issue 1 Answer

problem setting rotation 1 Answer

Unity Bug? Parent object rotates around child object. 1 Answer

Camera Quaternion.RotateTowards without rolling/banking 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