AI systems using Unreal Engine 4: part 1

May 28, 2020 at 12:00 pm by nemirc

AI systems in Unreal Engine 4

As you may have seen in my Unreal Engine 4 series, one of my interests is making a working AI for my project “Just Let Me Go.” Now I am going to take a closer look at the AI system in UE4, and I also plan to share what I’ve been doing to create my custom AI. Just a clarification, this series will refer to my NPC as either “enemy” or “stalker” because “Just Let Me Go” is inspired by the game “Haunting Ground” and, in that game, enemies are called stalkers.

Before I continue, I need to mention this article requires a moderate level of Blueprints knowledge to be understood.

In my main UE4 series I mentioned that the AI system is comprised of different parts: the AIPerception system for “sensing” (seeing, hearing and sensing the player), an AIController to control the character, a Blackboard for data storage and a Behavior Tree to tell the NPC what to do. First, I added the AIPerception system to my base enemy class, as you see in the image below.

However, I quickly realized the sight cone completely ignored body movement and, more importantly, face orientation. For that reason, I decided it was better to attach the AIPerception to an empty object (which I called “Stalker Senses”), and then attach that object to the enemy’s head. This may sound like I am over-complicating things, but actually this will make the enemy more realistic. If, for example, the enemy turns its head to the right, it would be logical that the sight cone also turns to the right, rather than keep facing forward.

To attach “the brain” to my stalker, I used a “Child Actor” component parented to the character’s head, and then, I used my “Stalker Senses” as the Child Actor class. To pull a reference to the senses, I used a construction script to create the reference. That way, I can pull data from my Stalker Senses object to my enemy actor. The final setup is seen below:

And this is how the character looks with the Stalker Senses object attached to the head.

My Stalker_AIController is also checking if the player is seen or heard. The following function is an example of how I set up the sight check.

The actual perception is a little bit more complicated. Basically, the AIPerception’s perception event is fired every time it “perceives” something (sight or hearing), and the trick is getting what kind of stimulus was created (sight or hearing), and also what object created that stimulus.

At this point I had to create a custom function called “Stimulus Type” to figure out what kind of stimulus was perceived.

Stimuli types correspond to an array. Since I created two, sight and hearing (in that order), a stimulus with the index 0 means sight and one with the index 1 means hearing. Using the “Switch On Int” node in Blueprints lets me output the “Is Seen” or “Is Heard” variables as results of my “Stimulus Type” function. These “Is Seen” and “Is Heard” values are the ones I am pulling from my AIController class using the function I described 3 paragraphs above.

And this is the output of the function. As you see, it has the flags and also the location of the player when it was perceived.

At this point, you also need to have your AIController attached to your enemy. I created my own AIController class (called Stalker_AIController) derived from the base AIController class included in UE4, since I needed to customize it. At first, all I needed was to use my AIController class to attach my Blackboard and Behavior Tree to the character (you need to create these as well, even if they are empty at the moment). I used a BeginPlay event to create those attachments, but you can also use the construction script for that.

Next, I will describe how I configured my Blackboard and Behavior Tree to have a basic working AI.

Get Unreal Engine: https://www.unrealengine.com/






Sign up for our newsletter

This website uses cookies to ensure you get the best experience possible More Info
Got it!