Comments and answers for "Get rotation from a vector direction."
http://answers.unity.com/questions/1382256/get-rotation-from-a-vector-direction.html
The latest comments and answers for the question "Get rotation from a vector direction."Comment by Pakislav on Pakislav's comment
http://answers.unity.com/comments/1382319/view.html
Yes, I inverted AB because I realized it was originally the opposite of what I intended to use it for, which was the correct result of the Cross after all.
Thanks a lot for taking your time to explain this to me. Can you push me in the right direction to figure out how to turn the direction of the calculated vector into a rotation I can set as target for Quaternion.SLerp or equivalent method of rotating over-time? And maybe suggest what would be the best method of rotating that avoids the "never quite reaches 1" problem of SLerp?
Again thank you so much for your time!
EDIT: I believe Quaternion.LookRotation is what I'm looking for. Now to find something better than SLerp!Fri, 21 Jul 2017 12:35:36 GMTPakislavComment by Bunny83 on Bunny83's comment
http://answers.unity.com/comments/1382306/view.html
Well your two examples are very confusing since you use the vector AB for the quaternion rotation but the vector BA for the cross product. First have a look at your quaternion rotation:
You take the direction AB and rotate it 90° to the right since the quaternion rotation around Y is clockwise when you look at it from the top. It's counter clockwise when you look along the Y axis (from the bottom up). So T will be a vector that is 90° to the right.
Your cross product on the other hand will also rotate the vector 90° to the right but since you use the opposite vector (BA) you actually rotate the vector 90° to the left. That's why "T" is the inverse of "Turn":
T = -Turn
Keep in mind that Unity uses a left-hand system so the cross product uses the left-.hand rule instead of the right hand rule.
***edit***
To be more precise:
Vector3 AB = B - A;
Vector3 T = Quaternion.Euler(0, 90, 0) * AB;
Vector3 turn = Vector3.Cross(Vector3.up, AB);
In those two cases both "T" and "turn" should contain the same direction (roughly, keep in mind floating point numbers!).
ps: In a 2d environment the cross product can be extremely simplified since you use (0,1,0) as first vector. That basically leaves you with:
Turn90Right = new Vector3( AB.z, 0, -AB.x);
Turn90Left = new Vector3(-AB.z, 0, AB.x);Fri, 21 Jul 2017 12:11:29 GMTBunny83Comment by Pakislav on Pakislav's answer
http://answers.unity.com/comments/1382299/view.html
Thanks for your answer!
A and B are raycast on terrain in the X and Z plane. What I want to do is get an A -> B vector, rotate it 90 degrees left and use that to rotate the units - like in Total War games. But I'm failing to understand this.
Will Vector3.Cross(Vector3.up, A - B); get that A -> B vector turned by 90 degrees left?
This code:
Debug.Log("A = " + A + " B = " + B);
Vector3 AB = B - A;
Vector3 T = Quaternion.Euler(0, 90, 0) * AB;
Debug.Log("T = " + T + " AB = " + AB);
Vector3 turn = Vector3.Cross(Vector3.up, A - B);
Debug.Log("Turn = " + turn);
Prints these values:
A = (-18.3, 0.0, 16.2) B = (3.2, 0.0, 18.7)
T = (2.6, 0.0, -21,5) AB = (21.5, 0.0, 2.6)
Turn = (-2.6, 0.0, 21.5)
So which one will be my +90 degrees? T or Turn?
Thanks again for your help!Fri, 21 Jul 2017 11:48:18 GMTPakislavAnswer by Bunny83
http://answers.unity.com/answers/1382287/view.html
The success of this line:
Vector3 turn = Vector3.Cross(Vector3.up, A - B);
highly depends on which plane you use for your RTS game. This assumes that A and B are both inside the X - Z - plane. If you use the X - Y - plane (the usual 2d GUI plane) the cross product will result either in "Vector3.forward" or "-Vector3.forward"
In addition you should make sure that "AB" is actually a non zero vector. Or in other words: make sure your two hit points are not the same.Fri, 21 Jul 2017 11:23:19 GMTBunny83