Attaching to parent while keeping position relative

In code, I have been trying to figure out how to parent a rigid body child to an object while always keeping its position the same distance from the parent at all times, taking into account the parents rotation. For example, I create a sphere at 0,0,0 and attach a child sphere at 10,0,0 using transform.parent. If the parent moves 1 unit along X, the child would move 1 along X as well. If the parent rotates 90 degrees on y axis, the child would also rotate 90 degrees along the parents y axis.

Currently I am using fixed joints to accomplish this but I think there has to be a better way. I use Havok at work and they have an AttachToParent that keeps all the orientations and rotations relative to the parent without using joints.

I could easily be thinking about this the wrong way, but how can I accomplish this in Unity?

I’m pretty sure you are over thinking it. If you have an object 1 at (0,0,0), and object2 at (10,0,0) and you parent the object 2 to object 1 (by dragging object 2 onto object 1 in the inspector) and you then move object 1, object 2 will move with it. I don’t see why you would need joints for this.

To programmatically parent an object to another (so that it moves and rotates and scales with the parent object) just set child.transform = parentObj.transform.
A parent member is of type Transform, not gameobject. Also make note to parent the current script’s gameobject it is attached to, just use: transform.parent = otherGameObject.transform.

When you manipulate the parent, the child gameobject will follow as well, keeping offsets and such from before it was parented.

Parent Unity Doc