skip to Main Content

Creating AI with Perception

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:

navmesh

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:

bp_aichar1

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

ai_char

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:

blackboard

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:

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:

This Post Has 11 Comments

  1. Hi Orfeas,

    How do I set the minimum distance between the AI and the player? I’m making a shooting AI and the AI only shoots the player when it’s in front of the player.

      1. Oh my god, I feel so dumb, never noticed that before. Thanks for pointing that out! 😀

        – Predalienator

  2. Hi Orfeas,

    I love your tutorials but I wanted to note that when creating a blackboard key value, when you set it to object you have to set the base class of it to Actor or else it won’t show up in the Behavior Tree for Move To and such…. Just thought it would be a helpful addition to note when creating the Target key.

  3. Hmm, I can’t get this to work. I get no errors in code, behavior tree and blackboard is same as yours I have put nav mash and built path. Selected everything in blueprints but when I put BP_AICharacter in volume (or to say level) it dosen’t work. I added more GLog in code to see if everything is working but none GLog is firing (nither GLog’s, nither AI is moveing and nither ”Oh hello there). Any idea what might be wrong?

  4. Nvm, I realised my nav mesh was was above the bottom of AI’s capsule, that was reason why it didn’t work. How simple mistake can break whole functionalty.

    And I want to say thank you for doing this tutorial, It means so much I hope you are still adding tutorials for c++. Big thank you!

    Note: Code works in 4.15.

    -Thewale

  5. Hi Orfeas,

    Do you have any tip about the work distribution between AIController and Character? In your other tutorial you put UBehaviorTree in the AIController class, not sure if it was intended.

    I believe they can achieve the same result eventually, but what’s the pro and cons for putting more logic in AIController or in Character?

    1. Hello,

      Multiple characters may have the same AIController so you usually write the code tha handles the logic for the AI inside the controller and leave other properties that are not related to that (ie Health, Damage, MovementSpeed and what not) to the Character class. Having said that, typing the logic inside the character here wasn’t intended.

      -Orfeas

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back To Top