Home > Back-end >  How to detect if enemy is in the same lane as player in a 2d grid?
How to detect if enemy is in the same lane as player in a 2d grid?

Time:06-21

I have the position of the enemy and the player. I want to know if they're in the same lane, that is, the enemy should be ahead/behind the player, either horizontally or vertically. How can I go about implementing this?

I thought of doing a raycast in both horizontal and vertical directions to check if there's an enemy, but I don't want to do this because it will be too inefficient, since I already cast an overlap sphere to find the enemy.

CodePudding user response:

With your explanation this seems enought:

if (player.position.y == enemy.position.y or player.position.x == enemy.position.x)
        sameLane = true;
  • Related