School project / Fall 2025
Enemy AI in Unreal
Two enemy AI assignments in Unreal Engine 5.6.
- Role
- Solo
- Team
- Game Engines 2 course
- When
- Fall 2025
These assignments were about how the AI controller, the blackboard and the behaviour tree work together in Unreal.
First I wrote the enemy AI in C++. The enemy sees you, follows a patrol route, chases you, and goes back to patrolling a few seconds after it loses you. Then I made a second version in Blueprints, where the enemies wander around, find you, chase you and attack.
- In C++, gave the enemy sight out to 2000 units in a 90 degree cone, and started its behaviour tree when the game begins.
- In C++, made a patrol route you place in the level point by point, and the steps that walk the enemy from point to point.
- In C++, wrote a check that runs four times a second to see if the enemy can see you, and a timer that sends it back to patrol 3 seconds after it loses you.
- In Blueprints, made enemies that wander to random spots on the map and wait there.
- In Blueprints, added a check around each enemy for the player. Once it finds you, it runs at you, turns to face you and attacks, with a short wait between hits.
- In Blueprints, wrote an attack that damages whatever is in front of the enemy.
The behaviour tree
The tree first checks whether there's a target to chase. If there is, the enemy chases it, turns to face it and attacks. If not, it wanders somewhere and waits. The blackboard stores what the enemy knows, like who it's chasing.
Losing the player
In the C++ version the enemy remembers when it last saw you. After 3 seconds it forgets you and goes back to its route.
Code I wrote
Short parts of my own code from this project. I left out anything the course gave us.
Enemy sight
EnemyAIController.cpp, Unreal Engine 5.6 C++
AEnemyAIController::AEnemyAIController()
{
PerceptionComp = CreateDefaultSubobject<UAIPerceptionComponent>(TEXT("Perception"));
UAISenseConfig_Sight* Sight = CreateDefaultSubobject<UAISenseConfig_Sight>(TEXT("SightConfig"));
Sight->SightRadius = 2000.f;
Sight->LoseSightRadius = 2400.f;
Sight->PeripheralVisionAngleDegrees = 90.f;
Sight->DetectionByAffiliation.bDetectEnemies = true;
Sight->DetectionByAffiliation.bDetectFriendlies = true;
Sight->DetectionByAffiliation.bDetectNeutrals = true;
PerceptionComp->ConfigureSense(*Sight);
PerceptionComp->SetDominantSense(Sight->GetSenseImplementation());
}
void AEnemyAIController::OnPossess(APawn* InPawn)
{
Super::OnPossess(InPawn);
if (!BehaviorTreeAsset)
{
return;
}
UseBlackboard(BehaviorTreeAsset->BlackboardAsset, BB);
RunBehaviorTree(BehaviorTreeAsset);
// Seed PatrolPath & PatrolIndex into the blackboard once we possess the pawn
if (const AEnemyCharacter* EC = Cast<AEnemyCharacter>(InPawn))
{
if (EC->PatrolPathRef && BB)
{
static const FName KeyPatrolPath(TEXT("PatrolPath"));
static const FName KeyPatrolIndex(TEXT("PatrolIndex"));
// Explicit cast to UObject* keeps the compiler happy
BB->SetValueAsObject(KeyPatrolPath, Cast<UObject>(EC->PatrolPathRef));
BB->SetValueAsInt(KeyPatrolIndex, 0);
}
}
}
Line of sight check
BTService_UpdatePerception.cpp, Unreal Engine 5.6 C++
UBTService_UpdatePerception::UBTService_UpdatePerception()
{
Interval = 0.25f;
RandomDeviation = 0.f;
}
void UBTService_UpdatePerception::TickNode(UBehaviorTreeComponent& OwnerComp, uint8*, float)
{
AAIController* AI = OwnerComp.GetAIOwner();
UBlackboardComponent* BB = OwnerComp.GetBlackboardComponent();
if (!AI || !BB) return;
APawn* Self = AI->GetPawn();
APawn* Player = UGameplayStatics::GetPlayerPawn(AI, 0);
if (!Self || !Player) return;
const bool bLOS = AI->LineOfSightTo(Player);
BB->SetValueAsBool(CanSeePlayerKey.SelectedKeyName, bLOS);
if (bLOS)
{
BB->SetValueAsObject(TargetActorKey.SelectedKeyName, Player);
BB->SetValueAsFloat(LastSeenTimeKey.SelectedKeyName, Self->GetWorld()->GetTimeSeconds());
}
const float Dist = FVector::Dist(Self->GetActorLocation(), Player->GetActorLocation());
BB->SetValueAsBool(InAttackRangeKey.SelectedKeyName, Dist <= AttackRange);
}
Both are my own assignments. The C++ version builds, but its behaviour tree is empty in my saved copy, so the video shows the Blueprint version. I rebuilt it in Unreal Engine 5.6 to record it.
Next school project
Object pool
A C++ console program for my Object Oriented Design course.