Using the Apex Utility AI to Sample Positions on Unity’s NavMesh

Introduction

This document is a part of a series of tutorial documents for getting started with the Apex Utility AI.

The purpose of this document is to provide a description of the classes used in the scene “AI_Demo_4”, as well as a tutorial on how to create a simple Apex Utility AI with the ability to sample positions on Unity’s NavMesh.

AIContext

We need two properties in the AIContext:

  • The Transform of the Game Object that will perform the scanning, so that we can query what positions to scan from.
  • A list of positions, so we can store the positions we have scanned. These positions can later be used for a variety of cases, such as determining which position is the best location to move to.

PositionScanner

We need to derive from the Apex Utility AI class ActionBase, since we want the AI to execute a scanning of positions. As we can see in the code snippet below, the Execute method samples positions on the NavMesh within a radius of the Unit’s position and adds them to the list of positions in the Context. Note that for simplification, we consider all areas of the NavMesh walkable by the AI in this example.

PositionVisualizer

We need to implement a Visualizer to see the positions we have sampled. In the DrawGizmos method we loop through all the positions and draw a sphere at each of their locations.

Behaviour

We need to add the PositionScanner action to the Default Action, add the AI to the Utility AI Component, and add the Visualizer Component to the Unit. As we can see in the screenshot below, we have a Unit in the middle with sampled positions around the Unit.