Align Parent object using child object as point of reference

Hierarchy:

  • Game object 1 with child transform (empty gameobject as a child)
  • Game object 2 with child transform (empty gameobject as a child)

Goal:
Align game object 2’s position and rotation with the game object 1 transform. Game object 2 needs to be aligned via its child transform (empty game object with only a transform). Also want to end up with the same hierarchy as we started with as the final result.

Current Approach - hoping for more optimized solution or easier to read / shorter:

  1. make child of game object 2 the
    ‘parent’ of game object 2 (it keeps
    its relative position this way)

  2. move the now ‘parent’ to game object
    2 to the destination game object 1
    transform (via transform.position
    and rotation - easy enough)

  3. make the game object 2 the ‘parent’ of its original child object transform

Obviously changing the parent / child several times gets old to type (i can make it a function yes) but it still seems slow / not required. Also consider that both game object 1 and 2 are child objects to yet another object in the scene (for both organization and movement). I tried Transform.Translate as it seemed to be the correct thing, but it didn’t provide the correct results when testing, I could have just had something misplaced.

I can provide REAL code if required, this is a c# project but I can totally understand javascript, post whatever you feel is appropriate - THANKS!

Edit Image Attached
Note that if half the rod is missing (as shown in the image) the overlap is visible in the objects i’m using, thus why i mention the visual issue (besides potential collider, center of mass and other physics issues)

The following do not need a source parent. and target, targetChild don’t have to be direct parent/child. can be ancestor/descendant in the hierachy

public static void Align(Transform target, Transform targetChild, Transform source)
{
    if (target && targetChild && source)
    {
        target.rotation = source.rotation * Quaternion.Inverse(Quaternion.Inverse(target.rotation) * targetChild.rotation);
        target.position = source.position + (target.position - targetChild.position);
    }
}

Here is a bit of code that does what I think you want:

// tr1P - Game Object 1 parent transform
// tr1c - Game Object 1 child transform
// tr2P - Game Object 2 parent transform
// tr2C - Game Object 2 child transform
void Alignment(Transform tr1P, Transform tr1C, Transform tr2P, Transform tr2C) {

	Vector3 v1 = tr1P.position - tr1C.position;
	Vector3 v2 = tr2C.position - tr2P.position;
	tr1P.rotation = Quaternion.FromToRotation(v1, v2) * tr1P.rotation;
	tr1P.position = tr2C.position + v2.normalized * v1.magnitude;
}

Note I cannot think of anything particularly wrong with the way you are currently doing it.

I know this is an old question, but I came to a different solution. This also works well when tweening to position.

void Alignment(Transform t1P, Transform t1C, Transform t2) {
    t1P.rotation = t2.rotation * Quaternion.Inverse(t1C.localRotation);
    t1P.location = t2.position + (t1P.position - t1C.position);
}

This thread helped me solve a problem that was plaguing me for ages - namely lining up coordinate systems of two different 3d tracking systems - thanks a lot! I used EthanF4D’s method ultimately - see my reply to this question: Align HTC Vive Coordinate with OptiTrack System in Unity - Unity Answers