Course contentsShow
← Unreal Engine Game Development
Lesson 413 of 8806. AI, Navigation, and Decision MakingFree lesson

Implementing NavMesh Areas for Agent Types

Learners will define custom navigation areas to create specialized pathfinding rules for different character classes, such as flying versus walking units.

In this lesson, you will learn how to create specialized NavArea classes to force different AI character types to prefer or avoid specific paths across your navigation mesh.

Understanding Navigation Areas

By default, your AI follows the "path of least resistance." However, what counts as "easy" for a soldier is impossible for a flying drone. Navigation Areas allow us to assign "cost" values to regions of our geometry. When an AI calculates a path, it multiplies the distance cost by the area's traversal cost.

To implement this, you first define a custom NavArea class in C++ (inheriting from UNavArea). Within this class, you override the DefaultCost variable. For example, a NavArea_Water might have a high cost for walking units, while a NavArea_FlyOnly might be the only navigable surface for aerial units.

Applying Areas to the World

Once your class is defined, you have two ways to apply these rules:

  1. NavModifierVolumes: You can place these volumes in your level and assign your custom AreaClass to them. Any part of the NavMesh inside this volume will adopt that area's cost settings.
  2. Navigation Query Filters: This is how you distinguish between agents. When you issue a MoveTo command to an AI Controller, you can pass an FAIDefaultQueryFilter. By creating a specific filter for your "Flying" class, you can set the AreaClassFlags to ignore areas that are meant for walking, or significantly lower the cost of the NavArea_FlyOnly regions.

Implementation Logic

When your character makes a path request, the Navigation System checks the character's movement capabilities against the mesh's area metadata:

// Example of setting a filter override for a specific AI class
FNavQueryFilter* Filter = new FNavQueryFilter();
Filter->SetAreaCost(UNavArea_Water::StaticClass(), 10.0f); // Make water expensive
Filter->SetAreaCost(UNavArea_FlyOnly::StaticClass(), 1.0f); // Make flying cheap

By defining these rules, you ensure that your walking units naturally navigate around water or hazardous zones, while your flying units treat those same zones as highways.

Key Takeaway: NavMesh Areas use cost multipliers to influence AI pathing, allowing you to create diverse movement rules by pairing NavModifierVolumes with custom AI Query Filters.