- Home /
returning a relative Vector?
I have 3 vector3s and I need to get the relative Vector2(from V3 on the orange plane). How can I do this?
Sorry, I'll try clarify. So far I have generated these 3 vectors, Using Vector3.OrthoNormalize().
They are all based off V1 which I can move around in 3d space so it can be any direction(not just flat) I am happy with the way they relate to each other: As I can rotate them in space and they behave fine.
What I want is the Vector2 version of V3 considering V2 as the reference for UP or y axis and V1 is the z axis.
The plane that V3 is on is perpendicular to V1 and this plane (shown in orange). This is the relative plane that I want to derive my values from.
yes it would be easy if I used a gameObject, rotated it and used it's local axis, but you are right. I want to do just that without the use of a transform. hmmm... I look at some other forums and see what I find
All the math needed isn't difficult, but you need a concrete problem to be able to solve it. However I still don't get what V3 should represent. Your drawing looks like 3D space.
Do you mean all 3 vectors are Vector3-vectors and you want to project your the third one onto the plane defined by V1 and V2?
yes, all 3 are are vector3, and I already have V3 moving on the plane defined by V1 and V2, but I want the value of V3 as a VECTOR assu$$anonymous$$g that V1 is the relative UP, sort of like a local XY axis.
so AT$$anonymous$$ all 3 vector could be facing any direction in 3d space but I need the value of V3(relative to V2 as Up) to used as a UV offset(Vector 2).
Bunny83- Cheers I managed to get the effect I was after using InverseTransformDirection :) $$anonymous$$any thanks :)
Answer by Bunny83 · Jan 15, 2012 at 11:25 AM
Ok, i just re-read your clarification. Do you mean your plane's normal is your forward vector (red), V2(green) is your local upvector and V3 is an arbitrary vector that should be projected onto this plane? Some kind of head-up-display position?
If that's the case you can just use InverseTransformDirection of your transform and transform the vector3 into local space. Cancel out the z-part to project it to the local x-y-plane, that's all.
edit : Converted my comment into an answer ;)
second edit
If you want to convert the position manually into your own local 2d coordinate system, you need the two axis first. One axis would be your up-vector and the other missing vector your right-vector.
This one can be calculated with the cross-produkt(wiki). Since Unity uses a left-handed-system you have to swap the two vectors to get the right- instead of left-vector.
now you can simply project your desired vector onto those two (normalized) vectors to get the desired x-y coordinates:
// C# Vector3 forward; Vector3 up; // have to be normalized
Vector2 TransformPointIntoPlane(Vector3 aPoint) { Vector right = Vector3.Cross(up, forward).normalized; // If forward is also normalized you don't have to normalize "right" here. // If both of your axis-vectors are normalized you don't have to use Vector3.Project // the dot-product is enough: return new Vector2(Vector3.Dot(aPoint,right),Vector3.Dot(aPoint,up)); }
Answer by tylo · Jan 15, 2012 at 10:19 AM
Vector3 heading = (target.transform.position - transform.position).normalized;
This should give you the relative direction of one Vector to another. I might have those two terms backwards, I can never keep them straight.