Skip to content
Saarrthi Rana

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
Unreal Engine 5.6C++BlueprintsBehaviour trees
Video
The player grabs the rifle and runs while three enemies chase them across the level and swing at them. The enemy model isn't animated, so they stay in a T pose.
What it is

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.

What I did
  1. In C++, gave the enemy sight out to 2000 units in a 90 degree cone, and started its behaviour tree when the game begins.
  2. 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.
  3. 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.
  4. In Blueprints, made enemies that wander to random spots on the map and wait there.
  5. 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.
  6. In Blueprints, wrote an attack that damages whatever is in front of the enemy.
How it works

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

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++

Sets up what the enemy can see and starts its behaviour tree with its own patrol route.
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++

Runs four times a second and updates what the enemy knows.
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.