See if targeted ship is above, under, left or right of the script ship

Hey there!

So I’m trying to do an AI for the bots ships in my game, and I need to make a function that will make the ship look at the target ship. That part is under control, except for one thing:

I need to do is a function that allows me to see if the target is either under or above the ship and if its either at its left or right. So that when the FSM gets to that point, it makes my ship go left, right, up or down. (the ShipMovement.js already handles that, is just a matter os calling move.moveUp(), move.moveDown(), move.moveLeft(), move.moveRight());

So any orientation in this?

Thanks in advance. JPB18

One simpler way of making this calculation is translate the position of the ship into local space.

var v3 = transform.InverseTransfromPoint(target.position);

With the values in local space, you can check the sign of the x,y, and z components of v3.

  • x > 0 then v3 is to the right
  • x < 0 then v3 is to the left
  • y > 0 then v3 is above
  • y < 0 then v3 is below
  • z > 0 then v3 is in front
  • z < 0 then v3 is behind