How to do it...

We will create the main function for setting the ambush path for all the agents and then the function for setting each agent's path:

  1. Define the main function for the ambush:
public void SetPathAmbush(GameObject dstObj, List<Lurker> lurkers) 
{ 
    Vertex dst = GetNearestVertex(dstObj.transform.position); 
    foreach (Lurker l in lurkers) 
    { 
        Vertex src = GetNearestVertex(l.transform.position); 
        l.path = AStarMbush(src, dst, l, lurkers); 
    } 
}
  1. Declare the function for finding each path:
public List<Vertex> AStarMbush( 
        Vertex src, 
        Vertex dst, 
        Lurker agent, 
        List<Lurker> lurkers, 
        Heuristic h = null) 
{    // next steps 
} 
  1. Declare the necessary members for handling the extra cost of computations:
int graphSize = vertices.Count; float[] extra ...

Get Unity 2018 Artificial Intelligence Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.