Changing the Positions of Child objects to match their physical Positions

After importing an assembly from AutoDesk Inventor and importing it into Unity, I would like to create a method to show the user an “exploded” view of assembly. Moving each part away from the center to show its detail and relative position in what can be a very complicated assembly, in my case a robot.

Here is the code I am attempting to use:

{
    for(int i = 0; i < transform.childCount; i++)
    {
        Transform child = transform.GetChild(i);
        Vector3 dir = transform.TransformPoint(child.position) - transform.position;
        Quaternion dirQ = Quaternion.Euler(dir);
        child.Translate(dirQ * 2);
    }
}

However, since Inventor will convert the assembly as a whole, the parent objects is just an emptyGameObject in the middle of all of the child objects. Though this is not necessarily a problem, all the child objects’ positions are centered on the empty parent object. Such that even when the individual child objects are separated from the parent, their position is offset from the actual object.

For example: a wheel may be .2 meters from the center of the robot, but the actual center that unity uses is at the center of the assembly (the transform position reads 0,0,0 ), so it makes manipulation of these child objects very difficult.

Because of this, it seems that I am unable to create a vector3 from the middle of the assembly to any child object.

I would greatly appreciate if anyone who has solved this sort of problem before would share their solution. Or similarly, if anyone knows of another 3D modeling or CAD software that converts well to unity, please let me know.

Thanks a ton,

Michael Dobie, 868 TechHOUNDS

Assuming that each of your child parts (wheels etc.) have Renderer/Collider components on them, a quick’n’dirty way to get the center would be to use bounds.center. This won’t always give you the exact middle of the object though, as “bounds” represents an axis-aligned cube enclosing the entirety of the mesh/collider.

Something like:

for(int i = 0; i < transform.childCount; i++)
     {
         Transform child = transform.GetChild(i);
         Vector3 dir = child.GetComponent<Renderer>().bounds.center - transform.position;
         Quaternion dirQ = Quaternion.Euler(dir);
         child.Translate(dirQ * 2);
     }

The bounds center may not be accurate enough for you, but its a start.