I have 2 game objects. Game Object A and Game Object B. GO A is facing a direction and I want to know if GO B is on the left or right of GO A.
To illustrate my problem I made a picture:
The red line is the direction I'm facing (the z axis of GO A is pointing that way).
So how can I determine if GO B is facing to the left or right as seen from GO A, regardless of their positions in world space?
Use InverseTransformPoint:
var otherTransform : Transform;
function Start () {
var relativePoint = transform.InverseTransformPoint(otherTransform.position);
if (relativePoint.x < 0.0)
print ("Object is to the left");
else if (relativePoint.x > 0.0)
print ("Object is to the right");
else
print ("Object is directly ahead");
}
You could make two raycasts, one(1) from A directly forward and one(2) from A to B, and then compare the x components of their directions (I think that it would be left if 2.x < 1.x and right if 2.x > 1.x).