Search code examples
c#unity-game-engine2d

How to detect if enemy is in the same lane as player in a 2d grid?


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.


Solution

  • With your explanation this seems enought:

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