How to do it...

We will implement a function to evaluate a location given its height and surrounding points:

  1. Declare the function for evaluating the quality:
public static float GetHeightQuality (Vector3 location, Vector3[] surroundings)
{ 
    // next steps 
} 
  1. Initialize the variables for handling the computation:
float maxQuality = 1f; 
float minQuality = -1f; 
float minHeight = Mathf.Infinity; 
float maxHeight = Mathf.NegativeInfinity; 
float height = location.y; 
  1. Traverse the surroundings in order to find the maximum and minimum heights:
foreach (Vector3 s in surroundings) 
{ 
    if (s.y > maxHeight) 
        maxHeight = s.y; 
    if (s.y < minHeight) 
        minHeight = s.y; 
} 
  1. Compute the quality in the given range:
float quality = (height-minHeight) / (maxHeight ...

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.