How to do it...

This is an easy, yet powerful, function:

  1. Define the Smooth function:
public List<Vertex> Smooth(List<Vertex> path) 
{ 
    // next steps here 
} 
  1. Check whether it is worth computing a new path:
List<Vertex> newPath = new List<Vertex>(); 
if (path.Count == 0) 
    return newPath; 
if (path.Count < 3) 
    return path;
  1. Implement the loops for traversing the list and building the new path:
newPath.Add(path[0]); 
int i, j; 
for (i = 0; i < path.Count - 1;) 
{ 
    for (j = i + 1; j < path.Count; j++) 
    { 
        // next steps here 
    } 
    i = j - 1; 
    newPath.Add(path[i]); 
} 
return newPath; 
  1. Declare and compute the variables to be used by the ray casting function:
Vector3 origin = path[i].transform.position; Vector3 destination = path[j].transform.position; Vector3 ...

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.