1

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.

Incognito
  • 189
  • 2
  • 10

1 Answers1

0

You can always just set the arrow's rotations equal to each other, and as long as neither is parented to anything else, they will always be exactly equal, so long as the models themselves have been authored with the same axis point and initial rotation.

public GameObject arrow1;
public GameObject arrow2;

void Update(){

    arrow2.transform.rotation = arrow1.transform.rotation;
}

EDIT: If that's not working, how are you rotating the first arrow? Is it using transform.LookAt?

When I run into rotation issues it's usually a local vs. global issue. Maybe try:

void Update(){

    arrow2.transform.localRotation = arrow1.transform.localRotation;
}

Or maybe I don't understand what you're actually trying to achieve and a diagram could help clarify.

  • This was one of the first things I tried, but it only works if both planes are oriented the same way. My problem starts once one or both of the planes are rotated in a direction other than about the Y axis – Incognito Mar 01 '16 at 00:02
  • I've edited my question. I'm literally just manually moving and rotating the arrow at this point just to test (the second arrow responds immediately), but eventually it will be the main camera's rotation that needs to be mimicked, but at that point it's just moving the logic into that part of my code. – Incognito Mar 01 '16 at 17:57