In order to create a good and fun AI for your games many times you need to rely on the “senses” of the respective AI in order to determine it’s logic. For example, you may need to create an AI character which gets ready for a fight when he sees the player. Unreal Engine provides a set of AI senses (ie hearing, seeing, etc..) out of the box which need a little time to set up for your game.

In this post I will be using a Pawn Sensing Component (it can be used for “seeing” and “hearing” as well) in order to create a character that follows the player when he gets into the AI’s sight radius. For this post, I created a ThirdPersonTemplate project.

Setting up our project

When the editor is done loading the template project, I’ve added the following stuff:

  1. A Nav Mesh Bounds Volume which covers all the default map*
  2. A Blackboard which will hold a reference to the player
  3. A Behavior Tree which will contain the logic of the AI
  4. A C++ class, named AICharacter which inherits the Character class and
  5. A C++ class, named MyAIController which inherits from the AIController class

*In order to see the nav mesh that you’ve created press the P keybind and the accessible regions of your map will be marked as green like the following image suggests:


Setting up the AI Controller

Open up the header file of the AI Controller you created and type in the following functions and properties:

When you’re done with that, switch to your source file and type in the following code (don’t forget to change your includes if needed):

That’s it! We’re done for the AI Controller for this post!

Setting up the AI Character

Now that we have created everything we need for our controller, open up the header file of your AI character and type in the following code (you might have to give a different name to your constructor depending on how you named your class):

Once we’re done with that, switch to the source file of the character and add the following code:

Setting up the AI Character’s Blueprint

Now that we have a logic for our character, create a blueprint which inherits from the C++ class of the character we created above and make sure to:

  1. Add the default Mesh provided by the template project
  2. Rotate the Mesh in order to face the “aqua” arrow
  3. Set the Anim Blueprint Generated Class to the ThirdPerson
  4. Set up the reference to the Behavior Tree we created above
  5. Change the AI Controller Class to the AI Controller class we created above
  6. Select the Character Movement Component and set it’s max speed to 500 (optional)

Steps 1 through 3 can be reviewed in the following image:


Steps 4 and 5 can be reviewed in the following image:


Setting up the Blackboard and the Behavior Tree

For this example, we need only one key of Object type. Open your Blackboard and add in the following key:


Make sure to name your key with the same name as we provided for the corresponding BlackboardKey in the controller class as seen in the image above.

Now it’s time to set up our desired Behavior Tree. Below you can find a screenshot of my Behavior Tree:


You will notice that right above the Move To node there is a Blackboard Based Condition. This means that the Move To node will only be executed when the Blackboard Condition is true. In this case, we just make sure we have a valid Target in order to Move To.

To add a Blackboard Based Condition first add the Move To node. Then, right click and choose a decorator. A Decorator is essentially a condition. Then, choose the Blackboard option. The Editor will choose the first valid key of the blackboard which in this case is the Target key.

Testing our AI

At this point, we are ready to test our AI. To do so, just the AI Character somewhere in your map and click Play. When the AI sees you he will start to follow you. Here is my result: