Using Apex Path with the Apex Utility AI to Move

Introduction

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

The purpose of this document is to provide a description of the classes used in the scene “AI_Demo_5”, as well as a tutorial on how to create a simple Apex Utility AI with random movement using Apex Path.

Setting up the Scene

To use Apex Path, we need to have a Terrain GameObject with a Collider and a unit GameObject. Further, we need to create a GameObject and add the GameWorld component to it.

We do that by simply creating an empty Game Object and adding the required components by using the Game World Quick Start. This can be found in the Component list under Apex -> Quick Starts -> Navigation. Alternatively, we can create the game object automatically by choosing GameObject -> Create Other -> Apex -> Game World.

We need to ensure there is a layer for the Terrain in the list of layers in Unity. Subsequently, we need to find the LayerMapping component on the GameWorld GameObject and set the Terrain property to the Terrain layer. The layers are created automatically in newer versions of Apex Path. We also need to ensure that the Terrain GameObject’s layer is in the Terrain layer.

We need to ensure the Game World Grid is covering the Terrain GameObject. The Grid visualization will be entirely red if it cannot find a valid GameObject with a collider in the Terrain layer.

The process for creating a Unit that can navigate the Terrain with Apex Path is almost the same. You can ensure the Unit has the necessary components, by using the Quick Start Component option named ‘NavigatingUnit’.

Now that we have our three required Game Objects, we need to ensure that they are added to the correct Unity layers. Units should be in the Units layer and the Terrain should be in the Terrain layer, as mentioned previously.

AIContext

The AIContext class needs to have an IUnitFacade property. The UnitFacade provides an interface to the MoveTo method that the AI uses to move the Unit.

Tip
The UnitFacade also wraps many other aspects of unit handling for Apex Path and Apex Steer, including the radius of the unit, its attributes and group affiliations. In general the UnitFacade is the central access point for Apex Path and Apex Steer units.

Scorer

We only want the AI to get a new destination when it is not already moving.

Action

Since we added a Game World to the scene, we now have access to the Apex Path GridManager. We use this to sample the position we want to move to. This is shown in the below code snippet from the class ApexPathRandomMove. First the code evaluates whether the destination cell is walkable. Subsequently, the MoveTo method is called through the UnitFacade to actually prompt the unit to request a path and then move to the destination if there is a valid path.