This tutorial serves as a basic introduction for how to create a working AI character that uses a Behavior Tree to execute Blueprint Tasks.
1. Launch Latest version of Unreal Engine
2. Create a new Project. Select ÔÇ£Top DownÔÇØ
3. Name project something like ÔÇ£MyBehaviorTreeÔÇØ and press ÔÇ£Create ProjectÔÇØ Button.
4. Next add a NavMeshBoundsVolume to the scene and scale it to encompass the playspace. This volume is responsible to building navmesh that the AI will use to navigate.
Note: P toggles path visibility
Create a new Blueprint that uses the AIController class as its parent and name it BasicAIController
Create a new Blueprint that uses the Character class as its parent and name it BasicAICharacter
Open the newly created Character Blueprint and set its default AIController Class to the one your created in the previous step, (BasicAIController)
Finally add a mesh component to the Character Blueprint so we can see the character (*Note: Creating the AI Character automatically adds skeletal mesh under components, however, you can still add a static mesh component and ignore the skeletal mesh icon -but you cannot remove the skeletal mesh from the component list.)
Create a new BlackBoard Data Asset. This is done by right clicking inside the content browser then selecting Miscellaneous and then -> BlackBoardD (Name this asset BasicAIBlackboard )
The BlackBoard asset allows you to store information in keys that can then be used by the Behavior Tree. Create a key named TargetPoint and set its Key Type to Vector.
- In versions of UE4 before 4.5 you must first enable it in Edit --> Editor Preferences --> Experimental
In UE4.5, however, you no longer need to perform the previous step and can easily create a new Behavior Tree from the Content Browser as shown:
- Name this asset BasicBehaviorTree.
At this stage you should have the following 4 assets created.
BasicAiBlackBoard, BasicAICharacter, BasicAIController, and BasicBehaviorTree
We are going to create a simple Task for the Behavior Tree to execute. Create a new Blueprint using BTTask_BlueprintBase as its parent. Name this BasicTask
Next open up the created Task blueprint and go to its EventGraph. We need a Receive Execute event for when the task is called by the behavior tree and a Finish Execute event that returns success or failure upon task completion. Everything in between is task logic.
Before we fill in the logic for this task we need to create a public variable of type BlackboardKeySelector to store the random location that we find. Note: To create a public variable, make sure that the eye icon beside the variable is highlighted yellow and open. Do so by left clicking the icon. Name this Variable Destination
We want this task to take the AI Characters current worldspace location and find a random point within a set radius. So we get the Actors current location, find a random point within a specified radius, and then set the BlackBoard value to that location. Make sure to set the Radius of the Get Random Point in Radius to something large, like 2000, or else the AI may just be told to move an inch and therefore ignore the instruction. *Note: Make sure Success is checked under "Task Completion."
Open up the Behavior Tree and set it to use your created BlackBoard Asset
When the Behavior Tree is run it begins at the root and proceeds down the hierarchy executing tasks and returning successes or failures. For our simple wander example we are going to drag out from the bottom of the Root node and create a Sequence. From the bottom of the Sequence we are going to drag out and expand the tasks dropdown. Select the Blueprint Task Destination we created in the previous step. Then from the same sequence node drag out again and select the Move To TargetPoint. Your behavior tree should now look like this.
- IMPORTANT: Note that the acceptable radius here needs to be set to much lower, like 10 to 20, than the value we set up earlier for the AI CharacterÔÇÖs current world space when we set the Radius of the Get Random Point in Radius to around 2000. Setting the value too high here will result in the AI Character not moving at all.
Now, to run the Behavior Tree we have to call it from the AI Controller so in the Graph of your Blueprint AI Controller create the following:
Please note to select the Behaviour Tree asset from the drop-down box.
This will begin the Behavior Tree Simulation as soon as the Begin Play event is called.
The 2 easiest ways to preview the AI are to either place the AI Character blueprint in the level or use spawn actor in the level blueprint to create an instance of it.
To get your AI character to rotate based on movement enter the BasicAICharacter>Defaults tab. Find Use Controller Rotation Yaw and set this to false. Open the Components tab and find the Character Movement. Find Orient Rotation to Movement and set it to true.