Using the Apex Utility AI to Move on a Unity 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 to the classes used in the scene “AI_Demo_1”, as well as a tutorial on how to create a simple Apex Utility AI that moves to random positions using Unity’s NavMesh.

Setting up the scene

The scene should consist of at least three things:

  1. Load Balancer
  2. Terrain with a NavMesh
  3. Unit with a NavMesh Agent and a Utility AI Component

We need to create four classes in order for the AI to work:

AIContext

The AIContext is the memory and knowledge of the AI, it uses the context to make decisions. Inside our AIContext class we add all the information that we want the AI to know of, for example in this case we add the Nav Mesh Agent as a property (see the code snippet below). This will make the AI capable of navigating on the NavMesh. We can then use it to calculate paths by assigning destinations to the unit.

AIContextProvider

The AIContextProvider class holds a reference to the AIContext for the AI, and makes the AIContext available for the AI execution. The code snippet below is an example of such an AIContextProvider for a NavMesh:

Telling the Agent When to Move

To make the agent move, we need to create a Qualifier that will return a positive score when the AI is not moving. To do this we create a class entitled IsNavAgentNotMoving inheriting from the ContextualScorerBase class. In this class we define what the Qualifier’s score is based on. The score is used to determine what action to take. In this example we want the AI only to move when it is not already moving. As shown in the code snippet below, we make the score return 0 if it is already moving.

Moving the Agent

When a Qualifier is selected by the Utility AI it will execute its associated action. We create a class entitled RandomMove that inherits from the ActionBase class. In this example we just want the agent to walk to a randomly generated position on the NavMesh. Please note however that the code does not actually ensure that a valid path exists to the desired destination, instead it merely evaluates whether the position at the destination is walkable according to the NavMesh.

Configuring the Utility AI

The final step is to connect these classes together into a Utility AI by clicking:

Assets => Create => Apex => Utility AI

A window will then pop up asking for a “New AI Name”, which will be the name of the AI. After we have clicked OK, a screen with a Selector will appear.

We need to add a qualifier by right clicking the selector and choose “Add qualifier”.
In this case we will choose the most common type, “Sum of Children”, which will make the Qualifiers’ score the sum of all its Scorers. Next we can see the newly added Qualifier in the Selector. Now it is time to add a Scorer to the Qualifier, by selecting the Qualifier and clicking the Add button (a small plus) on the right, in the Unity inspector. The resulting selection window can be seen in the below screenshot:

Here we choose the IsNavAgentNotMoving Scorer we made earlier. We then change the score setting. Since the Default Action has a score of 1, we need to set ours higher, 10 for example.

Lastly we are going to add an action to the qualifier, by right clicking the Qualifier (not the Selector) and choosing the option “Add Action”. In this list we select the action class we made earlier called “RandomMove”. This results in an AI resembling the image below. By naming the qualifier in an intuitive way, we make it easier to understand and read the AI later on. In this case, we name the qualifier “Is Not Moving”, since that is the condition that renders it true.

Now that we have our AI we need to apply it to the Unit. If we click the Add button inside the Utility AI Component we can select our newly created AI.

After we have added the AI we can change some settings, such as the Interval setting, which will determine how often the AI executes. The first value is the minimum time for it to wait between executions, and the second value is the maximum. The AI will take a random value between the min and max value. If we set both values to the same value the interval will be constant. The “Start Delay” can be used to make the AI wait the specified time before it executes its first action.