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 sebas77 · Aug 03, 2012 at 02:55 PM · quaternionmathgeometry

SetFromToRotation vs SetLookRotation

Hi,

consider the following two lines:

 quat.SetFromToRotation(Vector3.up, newUpDirection);
 
 quat.SetLookRotation(Vector3.Cross(Vector3.right, newUpDirection));

The first operation should return a rotation matrix which rotates the transform matrix in such a way that the Y vector should coincide to the newUpDirection vector.

The second operation should return a matrix modeling a base where the Y is the newUpDirection and the forward is the cross product between right and newUpDirection.

In both cases, if I transform my object using the resulting matrices, I should have the same result. Instead they are different. I was not able to debug it visually, since I unit tested it, but now I will try to see what the real difference is.

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
2
Best Answer

Answer by Owen-Reynolds · Aug 03, 2012 at 03:44 PM

V3.Cross(up, right) gives negative forward (I can't remember the rule for direction of cross-products, so just tested it.) Your current result should face you exactly backwards.

Flip the order: V3.Cross( V3.right, newUp);

Comment
Add comment · Show 6 · 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 sebas77 · Aug 03, 2012 at 03:54 PM 0
Share

thanks for the head up, but my question was more about, why do I not get the same result from SetFromToRotation?

avatar image Owen-Reynolds · Aug 03, 2012 at 07:59 PM 0
Share

Put it this way, why do you think that two different functions with two very different inputs should give the same answer?

Once you work out all the math -- like what the CrossProduct is even doing there -- you'll see there's a small error. CrossProd's inputs are flipped.

avatar image sebas77 · Aug 06, 2012 at 10:02 AM 0
Share

you seem very good with math, you should know why the cross product is there.

avatar image sebas77 · Aug 06, 2012 at 03:58 PM 0
Share

changing the order the result becomes indeed the same, thank you :) However it is more correct to use SetFromToRotation

avatar image Owen-Reynolds · Aug 06, 2012 at 04:06 PM 1
Share

A lot of people only use transform.LookAt( actualWorldPos ); and have never seen a quaternion. For them, Quat.SetLookRotation is easier to understand -- it "aims" the quat the same way LookAt does.

Sure, FromToRotation is more versitile, so maybe "more correct". But, many people only ever aim Z. If they need to aim their Y, they use childing so they can aim the parent Z.

Show more comments
avatar image
-1

Answer by OSR_Dela · Apr 10, 2013 at 08:25 AM

Dudes, I am so sure that this is exactly the thing I need to figure out but honestly, I don't understand. There's some unspoken detail in here that you two gentlemen know that I do not.

I'm working in 2D, using X and Y for on screen movement and Z for sorting. So Vector3.Forward is actually my 'Down' and Vector3.Up is my 'Forward' - (0,0) is on the bottom left hand corner of the screen. Look at assumes +Z is the Forward and -Y is the down. I know I can child the GameObject containing all of the sprites to another GameObject that is already properly oriented to take advantage of LookAt()'s defaults but I don't want to do that for the rest of my life. I'd like to understand how you've told your transform that you are calling LookAt() on that it's up and forward have now changed so that LookAt() is oriented correctly to your custom configuration.

I tried:

 GOThatIsLooking.transform.LookAt( enemyTarget.transform.position );
 GOThatIsLooking.transform.rotation.SetLookRotation( Vector3.up, Vector3.back );

And:

 GOThatIsLooking.transform.rotation.SetLookRotation( Vector3.up, Vector3.back );
 GOThatIsLooking.transform.LookAt( enemyTarget.transform.position );
 

I've also tried defining my up in the optional argument like so:

 GOThatIsLooking.transform.LookAt( enemyTarget.transform.position, Vector3.back );

But that's not right. The Unity docs say that this second argument is more of a 'Suggestion' - which I think is because you really need two elements of the Vector to truly know what is forward. Seriously I'm an artist that's been coding for 10 years. I suck at math and have to use Wikipedia every time something requires Sin, Cos, or Tan.

Please forgive my ignorance here, I may even be barking up the wrong tree with these methods... It just seems there has to be a proper way to translate a LookAt()'s returned quaternion into a new orientation without using the dirty hack of childing the object to another re-oriented transform. I don't even want to understand the math or the why of it at all. I just want to know that if I use LookAt() and want to re-orient it for my coordinate system is that A) Possible? and B) What is the code snippet to accomplish this task.

I swear I have Googled and experimented well before asking this question. I don't want to program by coincidence here and arrive at a false solution that gives me gimbal lock in the middle of a demonstration - or some worse fate that you math heads know about but don't tell us artists so that we can sleep at night.

Comment
Add comment · Show 1 · 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 BerggreenDK · Aug 01, 2014 at 10:51 PM 0
Share

Sorry friend, but you dont post your question as a new Answer in this forum. This forum is about 1 question and multiple answers. Ask in the normal developer forum :-)

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

plotting a line which shows a spaceships course 0 Answers

Subtract square from polygon 0 Answers

Rotate object based on hit normal + matching camera direction 0 Answers

Inverse projection vertex shader 0 Answers

What is the rotation equivalent of InverseTransformPoint? 3 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