How to do it...

We will create the Waypoint class and add the methods for condensing the set of waypoints:

  1. Create the Waypoint class, deriving from both MonoBehaviour and from the IComparer interface:
using UnityEngine;using System.Collections;using System.Collections.Generic;public class Waypoint : MonoBehaviour, IComparer{  public float value;  public List<Waypoint> neighbours;}
  1. Implement the Compare function from the interface:
public int Compare(object a, object b){  Waypoint wa = (Waypoint)a;  Waypoint wb = (Waypoint)b;  if (wa.value == wb.value)    return 0;  if (wa.value < wb.value)    return -1;  return 1;}
  1. Implement the static function that checks whether an agent is able to reach one waypoint from the other:
public static bool CanMove(Waypoint ...

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.