GameObject Attachment Transformations

I know this is a fairly basic technique in 3D graphics so please forgive me if it’s a petty question.

I have two game objects, one is a parent of the other. I am trying to be able to detach them, rotate and move them about separately, and then re-attach them with the final resulting position and translation being based on the parent object, and the orientation of the two objects (in relation to each other) to be the same as their original positions/rotations.

Currently I am taking an offset of the two game objects as a ‘Transformation’ to be applied to the parent object when attachment is desired.

At startup:

_positionTransformation = transform.position - _parent.position;
_rotationTransformation = transform.eulerAngles - _parent.eulerAngles;

And during attachment:

transform.eulerAngles = _parent.eulerAngles + _rotationTransformation;
transform.position = _parent.position + _positionTransformation;

I should note that the attachment is happening on the child’s side, and as such it’s transform is being used as seen above.

This works fine when the parent object’s rotation remains unchanged; however, as soon as the parent object is rotated, the positioning of the child is no longer correct. The rotation of the child is correct in all simulations I’ve preformed so far.

My question is this. How do I get the position of the child object to attach back to the correct position on the parent relative to it’s new orientation and position?

I appreciate any hints and if you know of a good reference for picking up 3D graphics techniques that would also be much appreciated.

I was able to figure out a solution.

What I’m doing is storing the original Transformation information of both the child and the parent at startup. When I want to attach the two objects, I store the parent’s current Transformation in temporary variables, reset both parent and child Transformations back to their original state, re-attach the objects, and then restore the parent’s Transformation from the temporary variables.