I tried posting this in the unity forums, but it's been stuck awaiting moderation, so I'm posting it here in hopes of getting some more immediate feedback.
I have 4 objects, two planes and two arrows. One arrow points at one of the planes, and I want the second arrow to match the first arrow, but relative to the second plane. What I mean by this, is if the first arrow is moved 1 unit to the right relative to the first plane, the second arrow should move 1 unit to the right relative to the second plane. I have gotten the positions to work out correctly, but I can't figure out how to match the rotations. Some axes of rotations work correctly some of the time, but once one of the planes are rotated, the second arrow's rotation gets more out of sync with the first.
Here's the code that I'm using to test this, with the best I could figure out:
[ExecuteInEditMode]
public class RotationTest : MonoBehaviour
{
public GameObject target;
public GameObject camera;
public GameObject arrow;
void OnWillRenderObject()
{
if (!target || !camera || !arrow) return;
arrow.transform.position = target.transform.TransformPoint(transform.InverseTransformPoint(camera.transform.position));
//arrow.transform.rotation = Quaternion.Inverse(camera.transform.rotation) * transform.rotation * Quaternion.Inverse(target.transform.rotation);
//arrow.transform.eulerAngles = new Vector3(arrow.transform.eulerAngles.x, -arrow.transform.eulerAngles.y, arrow.transform.eulerAngles.z);
arrow.transform.eulerAngles = (target.transform.eulerAngles - transform.eulerAngles) + camera.transform.eulerAngles;
}
}
I put this script on the first plane, the first arrow is 'camera', the second plane is 'target', the second arrow is 'arrow'. When both planes are aligned exactly, the second arrow should be exactly the same position and rotation as the first. The commented out lines are the first thing I tried, the uncommented line is the next closest thing I got to work, but it only works correctly when the planes are rotated about the Y axis, and mess up when any other axis on the plane is rotated.
When both planes are facing the same direction, simply setting the rotation of the second arrow to the first arrow's rotation works correctly, but once the first plane is rotated this method falls apart.
I've been at this for quite a while, trying different combinations of Quaternion multiplications and differences of eulerAngles, but I can't seem to figure it out. Any help at this point is much appreciated.
Edit: Here are a few images depicting what I'd like to happen, and what is actually happening with the current script: https://i.stack.imgur.com/OErjQ.jpg The first three pictures show what is occurring when both planes have the same rotation (the arrow positions and rotations work correctly in this case, as I stated earlier). The 4th picture shows what happens (and should happen) when the plane is rotated about the Y axis. The blue arrow moves and rotates to look at the same point on the blue plane that the green arrow is now looking at the green plane. The last two pictures show what the blue arrow should do, but as can be seen, is rotating in odd directions. I'm testing this purely in the editor, so when I move or rotate any object, the blue arrow will respond immediately.