Skip to content
Tutorials Center
My learning

Topics

Lesson 32 of 38 ยท Animation and AI

AI Perception and Navigation

Give AI senses and paths in Unreal Engine. AI Perception sight and hearing, the Nav Mesh Bounds Volume, Move To, nav modifiers, EQS and an A-star pathfinding model in Python.

A decision tree is only useful if the AI knows what is happening and can get where it needs to go. Unreal Engine splits those jobs into two systems: AI Perception (what can the AI sense?) and navigation (how does it walk there?). Together with behavior trees they form the standard AI stack.

The Unreal Editor is not available on this site, so setup steps are described in words, the C++ is read-only, and the Python stand-ins model sight checks and pathfinding.

AI Perception

Add an AIPerception component to the AI Controller (not the pawn). Under it, add senses and configure each:

  • AI Sight Config: Sight Radius, Lose Sight Radius (slightly larger, so targets are not dropped at the edge), Peripheral Vision Half Angle (degrees), and Detection by Affiliation (enemies, neutrals, friendlies).
  • AI Hearing Config: Hearing Range. Noises are reported with UAISense_Hearing::ReportNoiseEvent or the Report Noise Event node.
  • AI Damage / Touch / Team / Prediction senses for other signals.

Things the AI can sense must be stimuli sources. Pawns are registered automatically for sight if they have an AIPerceptionStimuliSource component or Auto Register as Source is on.

React in C++ (or the Blueprint event On Target Perception Updated):

C++
AEnemyController::AEnemyController()
{
    Perception = CreateDefaultSubobject<UAIPerceptionComponent>(TEXT("Perception"));

    UAISenseConfig_Sight* Sight = CreateDefaultSubobject<UAISenseConfig_Sight>(TEXT("Sight"));
    Sight->SightRadius = 1500.f;
    Sight->LoseSightRadius = 1800.f;
    Sight->PeripheralVisionAngleDegrees = 70.f;
    Sight->DetectionByAffiliation.bDetectEnemies = true;
    Sight->DetectionByAffiliation.bDetectNeutrals = true;
    Sight->DetectionByAffiliation.bDetectFriendlies = false;

    Perception->ConfigureSense(*Sight);
    Perception->SetDominantSense(Sight->GetSenseImplementation());
    Perception->OnTargetPerceptionUpdated.AddDynamic(this, &AEnemyController::OnSeen);
}

void AEnemyController::OnSeen(AActor* Actor, FAIStimulus Stimulus)
{
    GetBlackboardComponent()->SetValueAsObject(TEXT("TargetActor"),
        Stimulus.WasSuccessfullySensed() ? Actor : nullptr);
}

Note the affiliation gotcha: by default only enemies are detected, and actors are neutral unless a team interface says otherwise. If sight seems broken, tick neutrals or implement IGenericTeamAgentInterface.

A sight check in Python

Sight is basically three tests: within radius, within the field of view, and with a clear line of sight (a trace). The stand-in implements the first two plus a simple wall check.

Python Stand-in: sight cone and radius

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The navigation mesh

For an AI pawn to walk around obstacles it needs a navigation mesh (NavMesh), a set of walkable polygons generated from your level geometry. Place a Nav Mesh Bounds Volume (Place Actors > Volumes) and scale it to cover the playable area. Press P in the viewport to show the NavMesh: green means walkable. If the volume is missing, Move To fails with no path.

Useful tools:

  • Nav Modifier Volume marks an area as non-walkable or assigns a navigation area class (for example Mud, with a higher cost so AI prefers going around).
  • Nav Link Proxy lets AI cross gaps (jump down a ledge).
  • Recast Nav Mesh settings in Project Settings or the volume's RecastNavMesh-Default actor: Agent Radius, Agent Height, Cell Size. The agent radius should match your character's capsule radius, or paths will clip walls.
  • Runtime generation set to Dynamic for levels where doors and obstacles change; Static is cheaper.
  • Navigation Invokers generate the mesh only around AI in large open worlds.

Moving the AI uses the Move To task in a behavior tree, or MoveToActor(Target, AcceptanceRadius) and MoveToLocation(...) on the AI Controller. Unreal finds a path with A* over the NavMesh polygons, then follows it with path-following logic that steers the character movement component.

A* in Python

A* explores the cheapest-looking paths first using the cost so far plus a heuristic estimate of the remaining distance. The stand-in runs it on a grid, where each cell is a tiny "polygon". Unreal's NavMesh uses polygons instead of squares, but the idea is identical.

Python Stand-in: A* pathfinding on a grid

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Environment Query System (EQS)

When "move to the player" is too simple, EQS asks questions of the world: "find a point within 1,000 cm that is behind cover, at least 500 cm from the player, and that I can reach". An EQS query generates candidate points (a Generator such as Points: Donut), scores them with Tests (distance, trace for cover, pathing), and returns the best one. Run it from a behavior tree with the Run EQS Query task and store the result in a Blackboard vector key. It is the right tool for flanking, cover and spawn-point selection.

Common mistakes

  • No NavMesh volume. Move To fails immediately.
  • Agent radius mismatch. AI gets stuck or paths skim walls.
  • Sight never triggers. Check affiliation, the perception component on the controller, and that targets are valid stimuli sources.
  • Setting SightRadius equal to LoseSightRadius. The target flickers at the boundary.
  • Running EQS every frame. Use a service interval or an event.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where should the AIPerception component normally be added?

    Perception belongs to the controller, which also owns the Blackboard and behavior tree.
  2. 2. Why is LoseSightRadius set larger than SightRadius?

    The extra distance gives hysteresis so targets are not dropped and reacquired repeatedly.
  3. 3. What makes Move To fail with no path in an empty-looking level?

    Without a NavMesh there is nothing to path over.
  4. 4. What is EQS used for?

    The Environment Query System generates items and scores them with tests to choose the best.

Summary

  • AI Perception (sight, hearing, damage and more) lives on the AI Controller; mind affiliation settings.
  • A Nav Mesh Bounds Volume builds the walkable surface; match the agent radius to the capsule.
  • Move To uses A* over the NavMesh; Nav Modifiers, Nav Links and area costs shape paths.
  • EQS picks smart locations such as cover or flanking points.

Next: Packaging and Build Configurations