While our game may be running without any issues in the editor or even in…
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:
- A Nav Mesh Bounds Volume which covers all the default map*
- A Blackboard which will hold a reference to the player
- A Behavior Tree which will contain the logic of the AI
- A C++ class, named AICharacter which inherits the Character class and
- 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:
- Add the default Mesh provided by the template project
- Rotate the Mesh in order to face the “aqua” arrow
- Set the Anim Blueprint Generated Class to the ThirdPerson
- Set up the reference to the Behavior Tree we created above
- Change the AI Controller Class to the AI Controller class we created above
- 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:
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.
The Move To node on your behavior tree contains a distance property.
-Orfeas
Oh my god, I feel so dumb, never noticed that before. Thanks for pointing that out! 😀
– Predalienator
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.
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?
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
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?
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
hey man, you have a circular dependency there
I want to make the AI return to patrol after player is out of sight, any ideas on how to make this?
[…] 6. Creating AI with Perception | Orfeas Eleftheriou […]
hi rfeas , I would like to know how to get ia back in patrol mode when it sees more the player?