Making the pathfining avoid a 1-tile radius

Thought I’d post this since it took me way longer than I’d like to admit to solve. I nearly asked for a solution, but I’m happy that I solved it myself.

I wanted my pathfinding to avoid a 1-tile radius “influence” zone so you cannot stop or move through it unless certain phase (to be implemented). This is similar to a mechanic in the Warhammer 40,000 tabletop game

So, if you want your pathfinding to avoid a 1-tile radius like I do and can’t figure it out, here you go! Any tips for optimizing or better implementing it are welcome. This is placed after if(!neighbourNode.IsWalkable) and before if (LevelGrid.Instance.HasAnyUnitOnGridPosition(neighbourNode.GetGridPosition()))

List<PathNode> pathNodeToCheckList = GetNeighbourList(neighbourNode);
bool hasFoundEnemyInfluence = false;
for (int i = 0; i < pathNodeToCheckList.Count; i++) {
               
  if (LevelGrid.Instance.HasAnyUnitOnGridPosition(pathNodeToCheckList[i].GetGridPosition())) {
     Unit detectedUnit = LevelGrid.Instance.GetUnitAtGridPosition(pathNodeToCheckList[i].GetGridPosition());

     if (detectedUnit.IsEnemy()) {
        hasFoundEnemyInfluence = true;
        break;                            
    }
  }
}
pathNodeToCheckList.Clear(); // Probably not needed, but better safe than sorry;

if (hasFoundEnemyInfluence) {
   closedList.Add(neighbourNode);
   continue;
}

Privacy & Terms