In this lesson, you will learn how to influence agent pathfinding behavior by assigning specific area types and cost multipliers to different surfaces in your scene.
Understanding NavMesh Areas
By default, the NavMesh treats all walkable surfaces as "Walkable." However, you can define custom NavMesh Areas (found in the Navigation window under the Areas tab) to categorize terrain—such as "Mud," "Water," or "Grass." Once you define these, you select your environment geometry and change its Area Type in the NavMesh Surface component or the NavMesh Modifier component.
Controlling Path Priority with Cost Modifiers
Simply labeling surfaces isn't enough; you must tell the NavMeshAgent how to interpret them. The Cost Modifier determines the "difficulty" of traversing a specific area.
Think of it like a GPS: if the highway is "Walkable" (cost 1) and a muddy swamp is "Mud" (cost 5), the agent calculates the path by multiplying the distance by the cost. If the path through the swamp is 10 units long (total cost 50) and the path around it via the highway is 30 units long (total cost 30), the agent will prefer the longer, "cheaper" highway route.
You modify these costs per agent using the areaMask and GetAreaCost / SetAreaCost methods:
// Example: Setting a high cost for the "Mud" area (index 3)
// This forces the agent to avoid mud unless absolutely necessary.
int mudAreaIndex = NavMesh.GetAreaFromName("Mud");
agent.SetAreaCost(mudAreaIndex, 5.0f);
Why this matters
This system allows you to create sophisticated movement patterns without writing custom pathfinding logic. You can make an enemy agent prioritize staying on roads, or simulate a heavy character avoiding water. By adjusting these values dynamically at runtime, you can make your game world feel reactive—for example, increasing the "Fire" area cost while a level is burning to force agents to navigate around the danger.
Key Takeaway: NavMesh Areas act as labels for your geometry, while Cost Modifiers allow you to mathematically influence agent decision-making, effectively prioritizing shorter, easier paths over dangerous or difficult terrain.