• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Vel_1828 · Jan 02, 2015 at 09:13 PM · aienemynavmeshnavmeshagentpathfinding

Horde of NavMeshAgents - stops to recalculate path.

PROBLEM:

I'm making a simple horde-shooter, where a big amount of Agents run towards a moving player. I've made a Navmesh through build-in Unity baker.

All works well, but the problem is - on larger distances (the map is an outdoor ruins complex), all Agents are stopping every few frames/seconds when the Player is moving.

DATA:

-It's obvious they are recalculating the path to the Player, as their SetDestination is set on Invoke Repeating (repeating at 0.1-0.8 sec random range - so the Update doesn't get clogged).

-The map is made in SketchUp and it's a solid, complex, single model (consisting of two levels, catwalks, mazes etc).

-The NavMesh baking takes ~2-5 seconds as it is.

THE QUESTION:

How can I speed up the path recalculating process on a big number (10-80) agents at once, so it's as smooth as possible?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by tanoshimi · Jan 03, 2015 at 02:52 PM

Well, your question gives several clues as to places where performance could be improved

  • Sketchup is notorious for creating high poly-count meshes which are not really game-ready. If your navmesh is baked from that, it too is probably overly-complex and should instead be baked from a low-poly version of the mesh.

  • It's good that you're not recalculating paths every frame, but even every 0.1 seconds seems high to me. Why not recalculate every 2 seconds or so?

  • And is it realistic that all your agents have exact information to home in on the exact player location, even when they are at the other end of the level and perhaps are completely visually obscured? If an agent is far away, perhaps just get them to navigate to the approximate vicinity of the player (which wouldn't require recalculation so often), and then only when they are close calculate the true path to the player.

  • In practice, few games calculate pathfinding for large numbers of individual agents, they'd use other techniques such as flow fields. It may simply be that you have to re-engineer your approach.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Vel_1828 · Jan 03, 2015 at 03:23 PM 0
Share

Thanks Tanoshimi for a reliable answer! I'll dig into the reasearch, as now I've got some very good hints to start with.

Yet I'm not making it as Answered, because it's a wider topic - maybe someone will add some more intel on overall optimalisation later. :)

avatar image Vel_1828 · Jan 03, 2015 at 05:49 PM 0
Share

O$$anonymous$$, looks like taking th timer down a bit and using a Polyreducer plugin for SketchUp helped a bit to extent of playability. Thanks again! :)

avatar image
3

Answer by The-Designer-Penguin · Jul 12, 2016 at 12:16 PM

I know this is an old thread, but this could be useful for some people: On your NavMeshAgent uncheck autobraking. This will not allow them to stop when recalculating the path.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dmg9626 · Aug 02, 2019 at 12:24 AM 0
Share

This was the fix for me (4 years later)!

avatar image
0

Answer by Foulcloud · Sep 23, 2017 at 05:15 AM

NavMesh.SetDestination is an asynchronous call but I don't think it is multi threaded. So I am guessing if the core this is using gets maxed out then it throws stuff in a queue and "gets around to it" when there is time/CPU.

My fix was to replace NavMesh.SetDestination with NavMesh.CalculatePath. Create a new path, set your agent to the new path and update the path periodically with NavMesh.CalculatePath. This is not asynchronous and you might lose a little performance.

Now your agents will not stop when system is taxed. Also, you can rely on the path and time the agent takes to get to the destination being consistent. Thinking of "Tower Defence" games where this would be critical.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by TimCoster · Dec 03, 2017 at 11:58 AM

With the help of the answer of Foulcloud and the unity documentation page about NavMesh.CalculatePath I came up with something like this script, that seems to work pretty well for a tower defence game I'm working on. The only thing you need to change to make the agent move to a new destination is changing the destination variable to something new, for instance in a coroutine:

using UnityEngine; using System.Collections; using UnityEngine.AI;

public class Agent : MonoBehaviour { public Transform target; public float pathUpdateFrequency = 2.0f;

 private NavMeshAgent navMeshAgent;
 private NavMeshPath path;
 private float elapsed;
 private Vector3 destination;

 void Awake ()
 {
     navMeshAgent = GetComponent<NavMeshAgent>();
 }
 
 void Start ()
 {
     path = new NavMeshPath();
     elapsed = pathUpdateFrequency;

     StartCoroutine(TravelToDestination());
 }
 
 void Update ()
 {
     elapsed += Time.deltaTime;
     if (elapsed > pathUpdateFrequency)
     {
         elapsed -= pathUpdateFrequency;
         NavMesh.CalculatePath (transform.position, destination, navMeshAgent.areaMask, path);
         navMeshAgent.SetPath (path);
     }

     for (int i = 0; i < path.corners.Length - 1; i++)
     {
         Debug.DrawLine (path.corners [i], path.corners [i + 1], Color.red);    
     }    
 }

 IEnumerator TravelToDestination ()
 {
     print ("Traveling towards destination..");
     destination = target.position;

     while (Vector3.Distance (transform.position, target.position) > 0.16f)
     {
         yield return null;
     }

     navMeshAgent.Warp(destination);
     print("Reached destination!");
 }

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

29 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make AICharacterController able to walk on walls and ceilings? 0 Answers

Nav Mesh Agent Not Moving Despite Successfully Creating a Path 0 Answers

Randomize NavMeshAgents path finding with same destination 1 Answer

Possible to edit the movement of NavMeshAgent ? 2 Answers

Ignoring or overruling a NavMeshAgent destination 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges