- Home /
Getting vector which is pointing to the right/left of a direction vector?
If I have a vector such as (0,0,1) and I get its 'right' equivalent I should get something like (1,0,0), its left would be (-1,0,0), this is just the simplest case there may be more complex scenarios such as (0.2, 0.3, 0.5), so on and so forth.
Any ideas?
Answer by aldonaletto · Mar 16, 2012 at 01:51 PM
In a 2D space - Vector2, or Vector3 with one coordinate fixed at 0 - you can get the right and left vectors with simple transformations:
// original vector in the xz plane (y = 0): var forward: Vector3 = Vector3(0.5, 0.0, 0.8); // transformed vectors right and left: var right = Vector3(forward.z, forward.y, -forward.x); var left = -right;For a full 3D vector, however, there's no predefined left/right directions - you must set some reference, usually the up direction. Having the up direction, you can calculate the right and left vectors using a cross product:
// original vector: var forward: Vector3 = Vector3(0.5, 0.7, 0.8); // up direction: var up: Vector3 = Vector3(0.0, 1.0, 0.0); // find right vector: var right = Vector3.Cross(forward.normalized, up.normalized); var left = -right;NOTE: I never remember the correct order of the Cross parameters; if the right vector is pointing to the left, just swap the parameters.
Thumb first!
Speaking of "The Left Hand Rule", the Vector Cross function takes the thumb parameter first. So I think your left and right are backwards :P
TEST: I threw this pretty little mess into an Update() of a script in order to check.
I also clicked 'Collapse' in the console... Played for a second, then stopped to read the console :P
Debug.Log("We feed the cross 'Up' then 'Forward' and get: "
+ "\n"
+ Vector3.Cross(Vector3.up, Vector3.forward));
Debug.Log("Now look! The world's 'Right' is also Vector: "
+ "\n"
+ Vector3.right);
Debug.Log("Just as you would expect from your left hand."
+ "\n");
Debug.Log("What if we feed 'Forward' first, as the ''thumb''?"
+ "\n");
Debug.Log("Awkward hand position, but you get: "
+ "\n"
+ Vector3.Cross(Vector3.forward, Vector3.up));
Answer by senad · Mar 16, 2012 at 12:51 PM
Use the vector cross-product: http://en.wikipedia.org/wiki/Cross_product
In the first case you would obtain it like this: bnrm = (0,0,1) x (0,1,0)
(although I am not sure if the sign would be right.) :D
Your answer
Follow this Question
Related Questions
How to move object using Vector3.Cross? 0 Answers
change the length of the vector 1 Answer
Vector 2 3 4 help 1 Answer
Vector based on the rotation of the main camera 1 Answer
Enemy and hero. Distance 1 Answer