0
\$\begingroup\$

I don't know how to decide if my point is on the left or right side of another object.

Diagram showing two configurations of object 1 & 2

My first thought was to check if x-axis of object1 is bigger or smaller than object2's, but if my objects rotate then that isn't working.

In the picture above we have two states where object2 is on the left side of object1. In one case its x coordinate is greater, in the other, its x coordinate is less.

\$\endgroup\$
1
  • \$\begingroup\$ "My first try was that if I check x-axis value bigger or smaller of obj1 and obj2," Elaborate on "it isn't working." That's the right way to do it, though you need to compare the transformed position of each object. \$\endgroup\$
    – 3Dave
    Jul 30, 2018 at 15:54

1 Answer 1

1
\$\begingroup\$

If you're in 2D, with the x+ axis to the right and y+ axis up, then given a direction vector v,

v_perp.x = -v.y;
v_perp.y =  v.x;

gives you a vector v_perp that's equal to v rotated 90 degrees to the left. Flip the signs to get a vector 90 degrees to the right.

So, you can do a check like this:

Vector2 positionOffset = object2Position - object1Position;
Vector2 rightward = new Vector2(object1FacingDirection.y, -obect1FacingDirection.x);


bool object2IsToTheRight = Vector2.Dot(positionOffset, rightward) > 0;

Of course, in Unity, if your two objects have transforms, then it's even easier since the rightward vector is pre-caclulated for you:

bool object2IsToTheRight = Vector2.Dot(
          object2.transform.position - object1.transform.position,
          object1.transform.right
     ) > 0;
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .