Hi there,
Just wondering if anyone could help me out here,
I have this script that is measuring the difference between the position of two objects. It’s really simple and it works fine (Base.position.y - Top.position.y)
But I am having trouble when they are on an angle like this. The two objects can’t be in a hierarchy but I want to measure the difference in position in this new co-ordinate system.
Vector3 v3distance;
float distance;
v3distance = Base.position - Top.postition;
distance = v3distance.magnitude; // Length of vector between Base and Top positions
Although a matrix may be easier to understand, Unity’s got several functions in the Quaternion class that will let you construct and use what you need, too. You’ll do your same Base.position.y - Top.position.y, and then rotate the resulting vector.
If you wanted to do it that way, you’d need Quaternion.Inverse(Base.rotation). However, only consider doing that if the coordinate system is fixed; otherwise, it will be too much calculation, when you could just use Transform.InverseTransformDirection. It’s possible the latter is fastest anyway, due to SIMD.
Hey Jessy,
The object is going to be rotating around during the game so i had a look at Transform.InverseTransformDirection.
For the angle in the image above Transform.InverseTransformDirection gives me Vector3(0.0,0.0,1.0). Which seems kinda unexpected I’m not sure how I could use it to get the result I’m after… Sorry to be a pain but I find this stuff a little hard to get my head around…
Every Transform has a local coordinate system, aka “local space”. All Transforms also exist in a common “world space”. Transform.InverseTransformDirection rotates vectors from world to local space. (If the Transform has no rotation or translation applied, its local space and world space are the same space.) Vectors are only directions with magnitude, so they can be rotated alone, to get from world to local. Positions, however, need to be both rotated and translated, which is where Transform.InverseTransformPoint comes in.
Hey Jessy,
I was using Transform.InverseTransformDirection in a weird way that was wrong for what I was trying to accomplish, that makes heaps more sense, thanks again for the help!