Best Practice For Detecting Walls

  • A* version: [5.1.2]
  • Unity version: [2022.3.33]

Hi, I am using grid graphs and working on a strategy game. Players can build walls around buildings. If enemy AI target a building that surrounded by walls, it has to attack nearest wall. After the wall is destroyed the ai will continue to target building. What function/components is the best for approaching this? thanks in advance.

Off the top of my head this might be relevant if everything is going to be an obstacle of some sort, but it is pretty new and I haven’t tried it yet.

If your using colliders you could also do raycasting, but I usually try to avoid physics for performance reasons.

I have lakes, mountains… so classic raycast does not work for me. I need to check it on my grid graph.

Sounds like that Astar Linecasting would work better then if those lakes/mountains and other stuff are going to be cut out of your grid graph. Here is the example from that documentation for Grid Graph

var gg = AstarPath.active.data.gridGraph;
var node1 = gg.GetNode(2, 3);
var node2 = gg.GetNode(5, 7);
bool anyObstaclesInTheWay = gg.Linecast(node1, node2);
1 Like

Super appreciate the community help here- I didn’t know this existed, thanks for pointing it out

Alternatively you can also search through all the nodes along the way to the building, and look for if this space is occupied by a wall. You can do this by checking your Path.path which will return a list of GraphNode. You can also use Path.OnVisitNode to check the next node by raycasting a bit forward… but that sounds a little more heavy than required.