# Detecting when objects are facing each other

I’m trying to detect when two objects are mirrored. I’m seen this question asked in the past and the best and simplest answer is checking if the dot product of the forward vectors is -1. Something like:

``````Vector3 rotation1 = obj1.forward;
Vector3 rotation2 = obj2.forward;
float fotProd = Vector3.Dot(rotation1, rotation2);
bool isMirrored = Mathf.Approximately(dotProd, -1);
``````

Unfortunately this isn’t working in all cases for me and now I’m getting confused about vector maths.

The problem is when two objects have rotations on the same axis only. For instance:

``````Obj1.rotation.eulerAngles: x:0 y:0 z:90
Obj1.rotation.eulerAngles: x:0 y:0 z:270
``````

If I get the transform.forward for those, they’re coming out the same: Both 0, 0, 1. Shouldn’t it be different, because if you worked out which way they were actually facing, they’d be the opposite to each other?

Then of course when I do the dot product, it does 1 * 1 and gets 1, not zero. Any hints would be very appreciated.

Debug.Log(Mathf.Abs(Vector3.Angle(transform.forward, target.transform.forward) - 180));

This seems a little messy but I whipped up something really quick that works well enough. I used Vector3.Angle.

``````  Vector3 lookDir = target.position - transform.position;

Vector3 myDir = transform.forward;
Vector3 yourDir = target.forward;

float myAngle = Vector3.Angle(myDir, lookDir);
float yourAngle = Vector3.Angle(yourDir, -lookDir);

if (myAngle < 5.0f && yourAngle < 5.0f)
{
print("facing");
}
``````

If someone else has a cleaner way but at least it’s a good starting point that works.

I’ve actually managed to solve my own problem, and for anyone in the future with the same issue I’ll explain it here, because it took me ages to work out.

Any 3D rotation will have one axis which just rotates the object, but doesn’t change the direction it’s actually facing. Imagine a wheel spinning on a car - it’s rotating but still facing the same way.

Now, normally in Unity this is a Z axis - hence my forward vectors were always coming out the same when I rotated Z. Unfortunately my objects were changing they way they faced when I rotated the Z, because I’d imported the models from Blender, and in Blender the Z axis is up instead of Y, meaning everything had a 90 degree rotation.

I decided to go into Blender and rotate my objects -90 degrees on the X axis, apply it, and then rotate 90 degrees and save. This then matches the objects visually between Blender and Unity but the 90-degree rotation that messed everything up doesn’t have to be applied, and the problem is fixed.

I imagine the problem could also be fixed by multiplying the forward vector by your actual forward vector.

I figured out this this
var value = Mathf.Abs(obj1.transform.rotation.y - obj2.transform.rotation.y);
this ranges from 0 to 1 if you rotate any of two objects
if both objects are completely oposite to each other value will be 1 and in the case, object 1 and 2 are parallel value will be 0