- Home /
Why navmesh area cost is not changing agent's path?
I have a grid of triggers that change the area cost for their respective sections depending on how many agents are in a section (I'm trying to recreate movement around crowds of agents). In the Navigation sidebar, it shows the area costs changing as the NavMesh agents move towards their destination, but the agents don't seem to change paths according to the higher area costs. Am I missing something here? Here's my code to keep track of how many agents are in each section:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class DensityCount : MonoBehaviour
{
public List<GameObject> agentList = new List<GameObject>();
public bool agentInZone;
void FixedUpdate()
{
if (agentInZone)
{
NavMesh.SetAreaCost(NavMesh.GetAreaFromName(transform.name), agentList.Count);
}
else
{
NavMesh.SetAreaCost(NavMesh.GetAreaFromName(transform.name), 1);
}
if (agentList.Count < 2)
{
NavMesh.SetAreaCost(NavMesh.GetAreaFromName(transform.name), 1);
}
}
private void OnTriggerEnter(Collider other)
{
if (!agentList.Contains(other.gameObject)) // if object is not in list
{
agentList.Add(other.gameObject);
agentInZone = true;
}
}
private void OnTriggerExit(Collider other)
{
if (agentList.Contains(other.gameObject)) // if object is in list
{
agentList.Remove(other.gameObject);
}
}
}
2019-10-26.png
(456.8 kB)
Comment
Have you tried smaller areas? Agents will only take the cost in account if they have other lower-cost options. They won't wait until the cost gets lower.
Your answer
![](https://koobas.hobune.stream/wayback/20220612213945im_/https://answers.unity.com/themes/thub/images/avi.jpg)