How to do it...

We'll be creating the graph representation class as well as a custom Vertex class:

  1. Create the VertexVisibility class deriving from Vertex:
using UnityEngine; 
using System.Collections.Generic; 
 
public class VertexVisibility : Vertex 
{ 
    void Awake() 
    { 
        neighbours = new List<Edge>(); 
    } 
} 
  1. Define the FindNeighbours function for automating the process of connecting vertices among them:
public void FindNeighbours(List<Vertex> vertices) 
{ 
    Collider c = gameObject.GetComponent<Collider>(); 
    c.enabled = false; 
    Vector3 direction = Vector3.zero; 
    Vector3 origin = transform.position; 
    Vector3 target = Vector3.zero; 
    RaycastHit[] hits; 
    Ray ray; 
    float distance = 0f; 
    // next step 
} 
  1. Go over each object and cast a ray to validate whether it's ...

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.