Using the Apex Utility AI to Identify and Select Targets

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_2”, as well as a tutorial on how to create a simple Apex Utility AI that targets one of three enemies based on time.

AIContext

In this context we need the AI to have access to two things: Its transform and the positions of the potential targets. So we add a Transform property and a GameObject array property, as can be seen in the code snippet below. This will enable us to set our transform to look at (rotate to face) the other GameObject’s transform.

AIContextProvider

As you can see in the code snippet below, we add a GameObject array field that we pass into the constructor of the TargetContext class. The benefit of this approach is that we can easily add the GameObjects via the “Target Context Provider” component through the Unity inspector for that component, e.g. through simple drag and dropping.

OptionScorerBase

In the TimedTargetScorer class we do not want to determine whether we should execute an action. Instead we want to determine what GameObject we want to target. By using the OptionScorerBase we can evaluate each of the GameObjects provided to the AI as options. Since we use GameObject as a type argument for the OptionScorerBase class, the Score method also has a GameObject argument that we can use for our evaluation, as can be see in the code snippet below.

ActionWithOptions

In the RotateToTarget class we have access to the GetBest method, as this method is inherited from the ActionWithOptions class, rather than the normal ActionBase class. The ActionWithOptions class facilitates evaluating the GameObjects provided as options and getting the one with the highest score, based on the list of OptionScorers chosen. We can then use that GameObject to get its Transform and rotate towards it, as shown in the following code snippet.

Behaviour

To set up the actual behaviour of the AI, we create a new Utility AI like we did in the last tutorial. This time we are not adding a Qualifier. Instead we add an action directly to the Default Action. The reason being is that we want to execute the action every time that the AI is run, unconditionally. So we add the newly created Action called RotateToTarget to the Default Action. In order to use the GetBest method inside the RotateToTarget class for evaluating the GameObjects, we need to add at least one Option Scorer. We can do that by clicking the Add button on the right, in the Unity inspector, and selecting the Timed Target Scorer. Note that this scorer needs to derive from OptionScorerBase or implement the IOptionScorer interface in order to show up in the selection window, as shown in the below screenshot:

Now that we have our AI we need to apply it to the Unit GameObject. Before we can test it we need to drag the potential targets to the Target Context Provider inspector field. In this demo we add three GameObjects: Target1, Target2 and Target3, as can be seen in below screenshot:

When everything is done, and correctly set up, we should have a scene with one AI that switches targets and rotates towards the ‘current’ target at every AI execution interval.