# Transforming Vectors from one space to another

Discussion in 'Scripting' started by Gibbonuk, Oct 11, 2019.

1. ### Gibbonuk

Joined:
Dec 10, 2013
Posts:
162
Hi, I am trying to port a old project of mine over from leadwerks to unity, the issue i am having is I use the following function in leadwerks a few times and i cannot find a way in unity to do so.

TFormVector - This functions allows you to convert a vector from one objects orientation to another.

However, in unity I can only find TransformVector and InverseTransformVector which takes you in and out of local and world, not one objects space to another?

Is there such function in unity, or anyone know how i can do it?

Thanks

2. ### jister

Joined:
Oct 9, 2009
Posts:
1,698
What do you mean with one objects space to another? Like you have a vector direction and want that same one starting from an other object? Or one objects worldspace to an other objects localspace?

3. ### Gibbonuk

Joined:
Dec 10, 2013
Posts:
162
Ill be honest im not 100% sure myself, I wrote it 13 years ago All i know is this.

Unity code to get the up transform of an object is this:
Code (CSharp):
1. gameobject.transform.up;
Code (CSharp):
1. body_up =  TFormVector(Vec3(0,-1,0),gameobject,nil)
but in my leadwerks code I also have this:
Code (CSharp):
1. TFormVector(Vector3,nil,gameobject)
and im just not quite sure what the equivalent is in unity...

From what I can find the params for TFormVector were:

Code (CSharp):
1. TFormVector(Vector3, GameObject src, GameObject dst)

4. ### Baste

Joined:
Jan 24, 2013
Posts:
4,428
That's what you need. To go from Transform A's space to Transform B's space, you move from A's space to world space and then from world space to B's space, ie.
b.InverseTransformPoint(a.TransformPoint(yourPoint)));

There's six methods, Transform/InverseTransformPoint, Transform/InverseTransformVector and Transform/InverseTransformDirection. The difference is what parts of the transform they take into account, so look through their docs to see what you need.

Note that you could also do some linear algebra - combine A's localToWorldMatrix with B's worldToLocalMatrix, and multiply your vector with that. That's probably not worthwhile to do, unless you're doing this a ton with the same A and B.

LiterallyJeff and lordofduct like this.
5. ### Owen-Reynolds

Joined:
Feb 15, 2012
Posts:
859
It looks like nil as the 2nd argument means to go local to world. In that case, Unity's versions are quicker (which should make sense, since Unity had the benefit of seeing those older awkward functions).

But it you use the local-to-local version lots, you might also write "TFormVector" in Unity (using the local A, to world, to local B method Baste describes).

unityunity