- Home /
Is it possible to store NavMeshAgents paths and assign them to other NavMeshAgents later?
Hi! So I'm developing a game in which in each level there are a given number of building (let's say 10, that is close to the average) that can spawn units. When a unit is born in a building, it goes to the building that has been set as target of that unit.
So far, I had the sistem set in this way: after player interaction, the unit is instantiated in the origin building, the target building is set as the destination in the NavMeshComponent of that unit and after the unit has its path calculated, it follows the path to the desired building where it is destroyed. So far so good, works.
The problem is that having 4 or 5 units going to their respective destinations at the same time causes a massive drop on the frame rate (from more than 120fps to less than 15fps).
So I figured that given that my building are static and there will not be dinamic obstacles, I could calculate all the paths between buldings at the beggining and them assign the correct path to each unit when it is instantiated. I don't know I this would help to increase the FPS.
I'm not interested in agents colliding with or avoiding each other, in fact I have the obstacle avoidance quality set to none. I'm aiming for Android and my goal is to have a maximum of about 30 units running smoothly (30 or more fps).
I can't build the paths manually using a waypont system because I plan to have many levels. So building the path from each building to the other 9 (about 90 paths per level if I have 10 buidings) would be insane.
Answer by ray2yar · Dec 28, 2018 at 03:01 PM
That's a great idea. Look into NavMeshPath and NavMesh.CalculatePath
Here's code that gets a path and assigns it to an agent.
public Transform Target;
NavMeshAgent Agent;
NavMeshPath Path;
void Start () {
Agent = GetComponent<NavMeshAgent>();
Path = new NavMeshPath();
NavMesh.CalculatePath(transform.position, Target.position, NavMesh.AllAreas, Path);
Agent.SetPath(Path);
}