How to check if an AI can reach a target?

Hey! In my game I’ve got repair bots which search for nearest machines to fix. Everythings set up expect the AI goes to the nearest machine even if the path is blocked for example if a machine is in a locked room. Is there a way to check if the AI can reach a target?

Someone else had a similar question before with no answers: How can I check if a target's position is reachable by my AI?

I’m looking for something like this (I made the “canReach” up as that is something I’m looking for);

List <GameObject> allMachines;

foreach (GameObject g in allMachines) {

   if (AILerp.canReach (g.transform.position) == true) {

       // This machine can be reached by the AI bot

   }

}

Thanks!

Hi

Yes, you can use PathUtilities.IsPathPossible.

Thanks! :slight_smile: What about getting the distance/length of a path so I can find the nearest target?

Hi

You could use the MultiTargetPath to do this.

I think something like this should work

// Disable the automatic path recalculation
ai.canSearch = false;
var path = MultiTargetPath.Construct(ai.position, targetPointsArray, null, null);
ai.SetPath(path);

Note that since you have to set ai.canSearch=false, the agent will no longer recalculate its path by itself, your script is responsible for that now.