How to do it...

This is a long recipe that could be regarded as a big two-step process. First, we build the Path class, which abstracts points in the path from their specific spatial representations, and then we build the PathFollower behavior that makes use of that abstraction in order to get actual spatial points to follow:

  1. Create the Path class, which consists of nodes and segments; only the nodes are public and are assigned manually:
using UnityEngine; 
using System.Collections; 
using System.Collections.Generic; 
 
public class Path : MonoBehaviour 
{ 
    public List<GameObject> nodes; 
    List<PathSegment> segments; 
} 
  1. Define the Start function to set the segments when the scene starts:
void Start() 
{ 
    segments = GetSegments(); 
} 
  1. Define the ...

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.