In this short tutorial, we’re going to see the basics of senses and stimuli sources using the Unreal Engine. Using AI perception makes it really easy for an AI to detect ennemies, or other objects. But while the Unreal Engine provides a good documentation on AI and behavior trees (here), there are few things on AI perception.

The basics.

The AI perception system allows an AI controller to perceive elements in its environnement. These elements are actors with the AIPerceptionStimuliSourceComponent attached. This component allow us to specify how the senses of an AI will be stimulated: for instance the stimuli source could stimulate the sight, the hearing, or any other sense of an AI.

On the other side, there is the AI. To make an AIController able to perceive stimuli sources, we have to attach it the component AIPerceptionComponent. This component describes how the AI is able to perceive its environment. For instance, we will use AIPerceptionComponent to define that the controller can “see” its environment (i will be stimulated by the sources stimulating the sight), and it’s also there that we will set the sense configuration (the radius and angle of vision, the perceivable types of sources, etc.).

Defining a stimuli source.

First, let’s define a stimuli source. We create a new actor and add it the component AIPerceptionStimuliSource.

In the component configuration, we have to tick the checkbox “Auto Register as Source” to register this as a stimuli source. Then, we have to add at least an element in the array “Register as Source for Senses”: it will define which senses are affected by this source. In our example, we add the AISense_Sight sense to indicate that this source will stimulate the sight of AIs.

Defining an AI perception.

Now, we make a pawn and we add it an AIPerceptionComponent.

We want to allow this pawn to “see” objects, so in the configuration of the AIPerceptionComponent we add a “Sense Config”.

Let’s have a look at the different parameters:

  • Implementation: it’s the type of sense, here “Sight”
  • SightRadius: the radius of the perception; objects out of this radius won’t be perceived
  • LoseSightRadius: if an object have been perceived, it won’t be perceived anymore if it goes out of this radius (this value is usually greater than the SightRadius)
  • PeripheralVisionAngle: the overture angle of the sight, if you set it to 360 an AI would be able to set everywhere around it, with a value of 90 it can only see forward.
  • DetectionByAffiliation: the kind of object that can be detected, by default it can only detect ennemies
  • AutoSuccessRangeFromLastSeenLocation: allows an AI to continue to see a source if it remains close to the last seen location, this range defines how close the source has to stay from its last location to keep being seen
  • MaxAge: the max age before a perception is forgotten (0 means never).

Important: in order to detect all objects, select all elements in DetectionByAffiliation.

Testing the perception system.

Before all, we will enable the perception debug mode. We have to go to Edit (top left) > Project Settings > Engine > AI System, then we tick the “Enable Debugger Plugin” checkbox.

When the game is running, we will press the apostrophe key on the keyboard to enable the AI debug view. Be sure to have the AI pawn in view when enabling this mode. Then, press the key 4 on the numpad to view the perceptions.

The whole tutorial project can be downloaded here:

Download link

First test: we put a pawn with the AIController and the actor with AIStimuliSourceComponent in the same map. We try to print a string when the source is detected and we also check the perceptions using the debug view.

As we can see, the pawn (on the left) is perceiving the stimuli source (on the right).

Second test: we try to put an obstacle between the AI and the source.

As we could expect from a “sight” sense, the stimuli source is not anymore perceived. Indeed, the obstacle prevent the pawn from viewing the source.

Conclusion.

In this short tutorial we have seen how to configure a stimuli source, an AI perception component, and how to test it using the debug mode. In the following tutorials on AI we will see more details about perceptions and how to work with other senses.