Unreal Engine 4 Tutorial: Artificial Intelligence
In this Unreal Engine 4 tutorial, you will learn how to use behavior trees and AI Perception to create a simple AI character that roams and attacks enemies. By Tommy Tran.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Unreal Engine 4 Tutorial: Artificial Intelligence
25 mins
- Getting Started
- What is a Controller?
- Creating an AI Controller
- Creating a Behavior Tree
- The Behavior Tree Editor
- What are Tasks and Composites?
- Moving to a Random Location
- Creating a Blackboard
- The Blackboard Editor
- Creating the Target Location Key
- What is a Service?
- Creating a Service
- Generating a Random Location
- Selecting a Blackboard
- Running the Behavior Tree
- Setting Up AI Perception
- Creating an Enemy Key
- Moving Towards An Enemy
- Setting the Enemy Key
- Creating an Attack Task
- Adding Attack to the Behavior Tree
- Combining the Subtrees
- Creating a Decorator
- Using Observer Aborts
- Where to Go From Here?
Using Observer Aborts
Observer aborts will abort a subtree if the selected blackboard key has changed. There are two types of aborts:
- Self: This setting will allow the attack subtree to abort itself when Enemy becomes invalid. This can occur if the Enemy dies before the attack subtree completes.
- Lower Priority: This setting will cause lower priority trees to abort when Enemy is set. Since the roam subtree is after attack, it is of lower priority.
Set Observer Aborts to Both. This will enable both abort types.
Now, the attack subtree can immediately go into roaming if it no longer has an enemy. Also, the roam subtree can immediately go into attack mode once it detects an enemy.
Here is the complete behavior tree:
Summary of attack subtree:
- Selector will run the attack subtree if Enemy is set
- If it is set, the Pawn will move and rotate towards the enemy
- Afterwards, it will perform an attack
- Finally, the Pawn will wait two seconds
Summary of roam subtree:
- Selector will run the roam subtree if the attack subtree fails. In this case, it will fail if Enemy is not set.
- BTService_SetRandomLocation will generate a random location
- The Pawn will move to the generated location
- Afterwards, it will wait for five seconds
Close BT_Muffin and then press Play. Spawn some muffins and prepare for the deadliest battle royale ever!
Where to Go From Here?
You can download the completed project here.
As you can see, it’s easy to create a simple AI character. If you want to create more advanced AI, check out the Environment Query System. This system will allow your AI to collect data about the environment and react to it.
If you want to keep learning, check out the next post in the series, where I’ll show you how to create a simple first-person shooter.