- Home /
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.
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);
thanks for the head up, but my question was more about, why do I not get the same result from SetFromToRotation?
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.
you seem very good with math, you should know why the cross product is there.
changing the order the result becomes indeed the same, thank you :) However it is more correct to use SetFromToRotation
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.
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.
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 :-)