- Home /
Multiple NavmeshAgents waiting each other to move to destination??
Hi there, I have a city and 100 navmeshagent citizens to roam in city. They take a random destination from script and go there. When they are close to their destination they take another random destination. But in order to move it looks like they are waiting each other like they are in a queue. They start their movement one by one and when they are close to target they stop and wait like they are in a queue. They all have their own scripts in their prefabs which allows them to move. Any of you know the solution to this problem? I couldnt find anything on the internet. Here is my source code:
public class FreePopulation : MonoBehaviour
{
public NavMeshAgent theAgent;
public float x;
public float z;
public Vector3 target;
public Animator animator;
// Start is called before the first frame update
void Start()
{
theAgent = this.gameObject.GetComponent<NavMeshAgent>();
animator = gameObject.GetComponent<Animator>();
RandomDestination();
}
// Update is called once per frame
void Update()
{
if (Vector3.Distance(transform.position, theAgent.destination) < 8)
{
RandomDestination();
}
if (theAgent.velocity.magnitude < 0.2f)
{
animator.SetBool("Idle", true);
}
if (theAgent.velocity.magnitude > 0.2f)
{
animator.SetBool("Idle", false);
}
}
public void RandomDestination()
{
x = Random.Range(-100, 101);
z = Random.Range(-100, 101);
target = new Vector3(x, transform.position.y, z);
theAgent.SetDestination(target);
theAgent.isStopped = false;
}
}
The problem with a random x and z positions is that Unity might be picking a position that is on top of the agent repeatedly and while Unity is testing the position, finding it below <8, setting a new position that is still <8, the agent isn't moving.
If you put a Debug message in RandomDestination and the identifier - you'd get an idea of how often it is running before the RandomDestination is far enough away to make the agent move.
I would suggest you test the Agent's position and set a random destination as a ratio of its position.
For example, if theAgent is on the left of the screen and must move to the right, set X to Random.Range (theAgent's position.x+10, the Agent's position.x+100)
You'd have to adjust my example to fit your layout and co-ordinates.
Thanks for your reply but I tried your thing and they are still stopping and waiting each other.I put them all in one place(at the center of map) but they moved in groups like first 6 moved out,stopped, second 7 moved out stopped... I tried my script on an empty plane without navmeshmodifiers and it works like a charm. I can not test if the target is inside of navmeshdata. That can be my problem. And originally the ones that are not moving take a randomdestination from script(I can see it in the editor) but does not move so I dont think its about < 8. Thanks :)
The only other thing I can think of is that the path calculations or something else in your game is slowing Unity down so much that you can see Unity doing one thing at a time. You could test this with profiling. I can imagine that with a lot of moving agents, calculating a path in order to avoid the other agents and the obstacles could use up a significant amount of resources.
Probably the solution, if that is the case, would be to move them from point to point rather than to a random destination Unity docs
Answer by firfur · Jun 04, 2021 at 03:30 PM
Found it, while I was baking the NavmeshSurface, it was only carving the outer lines of the buildings, so the insides were baking as walkable surface. So when theAgent gets a random destination and it is inside the house it cant reach it, cant find the way inside and stops for no reason.
Your answer
![](https://koobas.hobune.stream/wayback/20220613034510im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to move a single specific NavMesh agent? 2 Answers
Nav Agent Moving Only Forwards And Backwards 0 Answers
All my NavMeshAgents are tilted 1 Answer