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 yulaw2k · Aug 30, 2018 at 03:40 AM · rotationquaternion

Simple Quaternion.FromToRotation example please

Can someone post a SIMPLE example or examples of using this? Every time I read it about it I think it makes sense but I can never get it to work right.


How about something as simple as taking an objects forward direction, and matching it to another objects forward direction.


I thought it would be something like this.


Quaternion rotationDif = Quaternion.FromToRotation (transform.forward, target.forward); transform.rotation *= rotationDif;


This just produces crazy behavior everytime I run it.

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
2

Answer by JVene · Aug 30, 2018 at 06:11 AM

A study of quaternions and the utility of FromToRotation is a bit more than a post here can contain, but I can give you a start on what seems so confusing.


First, consider transform.forward. This gives a point one unit along the local Z axis of the transform. It is where the face is pointing. That much you probably know, but focus on that though a moment. What happens if you apply a local rotation on the z axis? The object 'rolls', but the forward position still faces the same point.


Now reverse the viewpoint of the question. Given a vector from the object's local origin, and assuming that's the direction the object should be facing, what would the local Z angle be? It could be any angle, because there's no information in this vector that could possibly tell you which way was "up" in that local coordinate system. The vector can only supply the direction to point from the face, but can't tell you what angle along that axis the object was 'tilted' to.


Armed with that, reconsider the code you posted. Forward, taken from transform, is compared to another object's forward from it's transform. There is no information from either vector to identify the rotation of the object around the forward local axis. Any such angle would be valid, as long as the angles were correct which pointed to that forward vector.


Quaternion rotationDif will provide what seems to be an angle of difference between the two 'forward' vectors, and it does, but it does not include any information about which way is up. As a result, the eulerAngles you'll get from the resulting rotation from your last line of code will not match the source. The face will point correctly, but the direction of "up" is not defined clearly. The larger the difference the "weirder" the result will seem.


If you change the source vectors from "forward" to "up", the results will be different, and for small adjustments may seem closer, but again there's lost information that you're expecting. In the case of up, yaw isn't clearly defined.


The problem is that you are expecting that the resulting quaternion in your last line of code should produce the same quaternion as the source object, but it can't. There isn't enough information to do that. If you were shooting a beam, which doesn't actually have a face to be concerned about, you'd get a rotation that provides a way to hit a target, but if that beam were a bullet, it doesn't matter how it "spins" along the axis it is moving - indeed, rifle bullets are supposed to spin to help improve accuracy. As that bullet spins the direction the bullet is traveling would not change. As the bullet is completely symetrical there's no "face" to be concerned with. Such may be a more suitable use for "FromToRotation", but you can't expect the result to include information which duplicates the entire quaternion after the rotation in your last line of code. You'll only "preserve" that portion of the information available from a pair of source vectors, which doesn't include the orientation of the source quaternion along the axis of your reference, such as 'forward' can't tell you which way is up.


What you're expecting would require the use of both forward and up vectors applied to SetLookRotation.


The actual utility of FromToRotation is a bit questionable beyond pointing to something. I don't think it is suitable for orienting one object to match another because not all information required is communicated through a pair of vectors.

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 yulaw2k · Sep 02, 2018 at 03:34 AM 0
Share

Thankyou that is great information, but then when would be appropriate to use FromToRotation?

avatar image JVene yulaw2k · Sep 02, 2018 at 07:35 PM 1
Share

Based on the limitations I cited, it only applies well where the objects involved are cylindrically symmetrical along the axis of reference (in your case that was forward). This would be a bullet, a ray or a direction without regard to the 'roll' rotation along the destination axis. There are various calculations that require a difference in the angle between two vectors, and this expresses such a calculation as a Quaternion, but frankly I'm more familiar with other means of using such a 'difference', like reflection and refraction. I believe use cases for FromToRotation are rare, with perhaps better alternatives.

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

180 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

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

Using a directional vector to orient a sphere? 0 Answers

Quaternion AngleAxis on pitch give random small value on others axis 0 Answers

How to make camera moving as the player on a planet 0 Answers

2D TopDown rotating a gun according to its parent position, ON MOBILE, not PC, 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