There's more...

We called the BuildPath function, but we haven't implemented it yet. It is important to note that this function is called by almost every other path-finding algorithm in this chapter; that's why it's not part of the main recipe.

This is the code for the BuildPath method:

private List<Vertex> BuildPath(int srcId, int dstId, ref int[] prevList) 
{ 
    List<Vertex> path = new List<Vertex>(); 
    int prev = dstId; 
    do 
    { 
        path.Add(vertices[prev]); 
        prev = prevList[prev]; 
    } while (prev != srcId); 
    return path; 
} 

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.