title: Capture position and rotation of an object relative to another object
body: <p>I have two identical objects and two cameras. What I'd like to achieve is the following:</p>
  
  <p>1) Capture the position and rotation of Cube A relative to Camera A</p>
  
  <p>2) Transfer that to a Cube B, so that in Camera B (which I cannot move), Cube B looks exactly as Cube A looks in Camera A</p>
  
  <p>I was successful doing that with position with the following code:</p>
  
  <pre><code>positionDifference = CubeA.InverseTransformPoint(CameraA.transform.position);
  </code></pre>
  
  <p>To transfer it onto Cube B, I do:</p>
  
  <pre><code>cubeBpos = transform.InverseTransformPoint(CubeB.transform.localPosition);      
  
  while ( Mathf.Abs (cubeBpos.x + positionDifference.x) &gt; 0.01f ) {
      if (cubeBpos.x + positionDifference.x &gt; 0) {
          CubeB.transform.position += new Vector3(-0.01f, 0, 0);
      } 
      else if (cubeBpos.x + positionDifference.x &lt; 0) {
          CubeB.transform.position += new Vector3(+0.01f, 0, 0);  
      }
      cubeBpos = transform.InverseTransformPoint(CubeB.transform.position);
  }
  </code></pre>
  
  <p>That's clunky, but works. However, when I try to transfer rotations, Cube B starts to pivot around the origin. Interestingly, when I move Cube A in world coordinates, Cube B moves in local, and vice versa. I suspect local-to-world coordinate translation is an issue, but I also think my rotation code is naive. I tried to capture rotation in two ways, first like this:</p>
  
  <pre><code>rotationDifference = Quaternion.Inverse(CubeA.transform.rotation) * CameraA.transform.rotation;
  
  CubeB.transform.rotation = Quaternion.Inverse(rotationDifference);
  </code></pre>
  
  <p>Second attempt:</p>
  
  <pre><code>rotationDifference = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, transform.eulerAngles.z);
  
  CubeB.transform.eulerAngles = rotationDifference;
  </code></pre>
  
  <p>Both approaches resulted in weird rotational offsets. I tried using localPosition and localEulerAngles, didn't help.</p>
  
  <p>I'm hoping there's a smarter way to do this :)</p>
  
  <p>EDIT:
  Here's a Dropbox <a href="https://www.dropbox.com/s/kjivdth96q3g6qd/AngleDiffTesting.zip?dl=0" rel="nofollow noreferrer">link</a> to the project</p>