Relative position of an object including rotation?

SOLVED

I need to calculate the relative position of an object as projected to another “proxy” position in space (it’s for lighting in a space scale scene). The catch is that the primary object in question can rotate, which is causing me grief.

I’ve built a small example scene just for testing purposes that looks like this:

So the Tracked Object is the object we’re trying to calculate the relative position of from the Primary+Offset objects. The proxy container is somewhere way off in space.

So to position our proxy object in the proxy container is actually quite simple:

public class ProxyController : MonoBehaviour
{
   public Transform trackedObject;
   public Transform primaryObject;
   public Transform offsetObject;

   public Transform proxyContainer;
   public Transform proxyObject;
   public Transform proxyCamera;

   private void LateUpdate()
   {
       // find our relative coordinates
       Vector3 worldPosition = primaryObject.position + offsetObject.localPosition;
       Vector3 relativePosition = trackedObject.position - worldPosition;

       // project that to our proxy object
       Vector3 proxyPosition = proxyContainer.position + relativePosition;

       proxyObject.transform.position = proxyPosition;
   }
}

Which gives us what we want (proxy view on the bottom):

Our real object and our proxy object come to alignment at the same time as our primary body moves through space. Splendid. But there’s the problem: The primary body can both pitch and roll in a multitude of directions. Here it is simply pitched 30 degrees:

The relative position of the proxy is incorrectly placed because it’s not accounting for the rotation. I know I have to transform the point somehow (around their relative plane?) I just can not for the life of me figure out what combination I need to get the right transformed point.

Ideas?

Thanks.

Future searchers: solved it by placing the proxy body inside a proxy container and rotating that container by the inverse of the offset object’s rotation.

I wouldn’t mind knowing the pure math solution if someone knows off hand.