This project implements an autonomous AI Enemy character using Unreal Engine 5's Behavior Tree, Blackboard, and AI Perception systems. The AI transitions dynamically between an idle or roaming patrol routine and an active chase behavior depending on its sensory state.
The execution flow of the AI is driven by a root Selector node, which evaluates its child branches from left to right. Logic is split into two prioritized sequences based on player visibility:
- Guard Condition: This branch is wrapped in a Blackboard-Based Condition Decorator that checks if the boolean key
CanSeePlayerIs Set (True). - Flow Control: The
Observer Abortsrule is set toSelf. WhenCanSeePlayerbecomes true, this branch takes immediate priority over itself to execute cleanly. - Execution Flow:
- Move To: Pathfinds the AI directly to the actor reference stored in the
TargetActorblackboard key. - Wait: Halts the AI for
0.5supon reaching the player before looping the chase check.
- Move To: Pathfinds the AI directly to the actor reference stored in the
- Guard Condition: This branch is wrapped in a Blackboard-Based Condition Decorator that evaluates if the key
CanSeePlayerIs Not Set (False). - Execution Flow: Runs continuously as long as the player remains undetected.
- BTT_FindPatrolLocation (Custom Task): Samples the surrounding navigation mesh (
NavMesh), picks a random valid destination coordinate relative to the AI's origin, and writes it to thePatrolLocationkey. - Move To: Commands the character movement component to walk toward the coordinates stored in
PatrolLocation. - Wait: Pauses the AI at its destination for
2.0sto simulate a natural investigative delay before looking for a new point.
- BTT_FindPatrolLocation (Custom Task): Samples the surrounding navigation mesh (
The Blackboard asset (BB_Enemy) handles the volatile operational memory for the system. It tracks the following specific keys:
-
SelfActor(Object): Automatically references the AI character instance itself for self-context queries within tasks. -
TargetActor(Object): Stores the direct actor reference of the player character once a successful vision scan occurs. This serves as the target destination for the Chase branch. -
PatrolLocation(Vector): Stores the$X, Y, Z$ coordinates generated by the custom task, mapping the destination for the passive wandering routine. -
AIState(Enum/Int): Reserved for advanced state tracking or transition debugging. -
CanSeePlayer(Bool): Acts as the system’s primary conditional toggle. Turning this flag on or off controls the structural flow of the Behavior Tree decorators.
The AI's sensory input is handled through a localized AI Perception Component configured inside the AI Controller blueprint using visual raycasting.
- Vision Envelope: The AI continuously scans an area defined by its forward vision angle and maximum sight radius.
- Detection Filters: The
Sight Configfeatures explicit affiliation checkboxes (Detect Enemies,Detect Neutrals,Detect Friendlies) turned on so that the player blueprint class is detected properly by the component traces. - Trigger Mechanics (
On Target Perception Updated):- On Sight: When the player enters the visibility cone, the perception system fires an update event with a
Successfully Sensedstatus of True. The AI Controller instantly updates theCanSeePlayerboolean key to true and stores the player character reference inside theTargetActorkey, triggering the high-priority Chase sequence. - On Lost Line-of-Sight: When the player ducks behind solid cover or exits the maximum range envelope, the event updates with a
Successfully Sensedstatus of False. The AI Controller fires a cooldown delay loop. If the player stays out of sight, it clears theCanSeePlayerandTargetActorkeys, forcing the tree back into the low-priority Patrol branch.
- On Sight: When the player enters the visibility cone, the perception system fires an update event with a
You can watch the fully working AI implementation and behavior state transitions here:
Watch the Demonstration Video on YouTube