- Home /
 
On a twice nested object, how to get the local position in terms of the middle parent space
Hi,
So, imagine a cube. This is the larger parent. This cube is made up of a bunch of smaller cubes in a 3x3x3 formation. Each of these smaller cubes is a smaller parent made up of six faces. Large Cube > smaller cubes > faces.
I need the positions of the faces. transform.position gives me their world position, transform.localPosition gives me their position local to the smaller cubes. I want to get their local position in terms of the larger cube space.
If I have to I can take the local positions of the faces in terms of the smaller cubes and convert them with some math, but is there a simpler way?
Thanks!
Answer by Bunny83 · Jun 12, 2016 at 11:04 PM
That's quite simple. The process is always the same:
 // given
 Transform parentObj;
 Transform nestedChild;
 Vector3 localPointInNestedChild;
 
 Vector3 worldPoint = nestedChild.TransformPoint( localPointInNestedChild );
 Vector3 localPointInParent = parentObj.InverseTransformPoint( worldPoint );
 
               If you just want the position of an object in local space of another object you can directly use:
 parentObj.InverseTransformPoint( child.position );
 
               edit
If you need to transform a lot points from one space to another you can simply create a conversion matrix by combining the right worldToLocal and localToWorld matrices.
Keep in mind that matrix multiplications work in reverse order. So if you have a source space and a destination space you would do:
 Matrix4x4 m = dest.worldToLocal * source.localToWorld;
 
               Now you just have to multiply each point with that matrix:
 Vector3 pointInDest = m * pointInSource;
 
              Your answer
 
             Follow this Question
Related Questions
Set child position and move parents with 2 Answers
Adjusting localPosition 1 Answer
Find a gameobject with the same position 2 Answers
Scripted tank track bones behave strange 0 Answers
Record Object's Position 2 Answers