How to determine if two Vector3s representing rotations are facing each other?

For a project I am working on, I have some rotation data in degrees stored as a Vector3 in an object. What I want to do is determine if two objects are facing each other.

The cases I’m evaluating look like this, in any given direction.:

[R][1 ->][<- 2][R] 1 and 2 are facing each other.
[<- 1][R][R][2 ->] 1 and 2 are not facing each other.

[R][1 ->]          1 and 2 are not facing each other.
[R]
[2]
[|]
[V]

The code I’ve found that should return this relationship is:

Vector3 normalized = (a - b).normalized;
float indicator = Vector3.Dot(normalized, Vector3.forward);  // Is this the right axis?
bool isFacing = indicator == -1.0f;

However, in the cases I’ve linked, I’ve had the following results:

[R][1 ->][<- 2][R] Are 1 and 2 facing each other?  Answer: No (Indicator = 0)
[<- 1][R][R][2 ->] Are 1 and 2 not facing each other? Answer: Yes (Indicator = 0)

[R][1 ->]          Are 1 and 2 not facing each other? Answer: Yes (Indicator = 0)
[R]
[2]
[|]
[V]

Based on the indicator number alone, I can conclude something is off. So, some questions:

Question 1: Am I using the right algorithm to determine if two Vector3 angles are facing each other?
Question 2: Assuming the answer to Question 1 is “Yes”, why isn’t my algorithm returning the expected results?

Finally, a caveat - I am not using Quaternion methods at all, because I can’t unit test my code if it’s present. This is for a procedural world generator, and I am writing unit tests A) because this sort of thing is difficult, and B) it’s actually testable; this is a purely input-process-output sort of thing that is not real time, and thus would give seriously diminishing returns to QA by playtesting alone.

If a and b are your direction vectors, your code should just be:

float indicator = Vector3.Dot(a.normalized, b.normalized);       
// Is this the right axis?
bool isFacing = indicator == -1.0f;

(untested, written from my hotel room at Unite Europe where noisy neighbours have woken me up!)

Vector3.Dot can be used to tell if one object is facing another. Just have one vector be the relative position of the other object and the other vector be the direction.

In case 1 one object is facing right and the other object is right of it

    Debug.Log(Vector3.Dot(Vector3.right, Vector3.right));

returns 1 signifying that there is 0 degrees between where the object is looking and where it should be looking meaning the object is looking at the other object.

In case 2 one object is facing right and the other object is left of it

    Debug.Log(Vector3.Dot(Vector3.right, Vector3.right));

returns -1 signifying that there is 180 degrees between where the object is looking and where it should be looking.

In case 3 one object is facing right and the other object is below it

    Debug.Log(Vector3.Dot(Vector3.right, Vector3.down));

returns 0 signifying that the is 90 degrees between where the object is looking and where it should be looking.

If you want to test if both objects are looking at the other then you’ll have to test both. After all it is possible for the first object to be facing the second while the second object is not facing the first.

In your code you use (a-b).normalized as one of the vectors. I’ll assume that this returns the direction vector of the other object. Then you use Vector3.forward as your other variable which I think is causing the problem. If the relative positions are right, left, down then forward is 90 degrees from all 3 which would explain why the indicator is 0 in all 3 cases. You probably want to use something like transform.forward (in this case the first object is always facing right so I used Vector3.right). And as I stated above, you’ll have to check if the second object is also facing the first.