Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
1
Question by AarshSinghVishen · Jun 16, 2017 at 02:44 AM · navmeshdistanceperformance optimizationmobileoptimizationnavagent

How many nav mesh agents can I have in a scene ?

All nav mesh agents will be checking the distance of the player from them,the player can control multiple units which also are navmesh agents. So performance wise, say I want to have the game run on mobile platforms, how many can I keep in a single scene? The Nav mesh will be small to mediu, sized ,i.e ,it won't be huge.

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

2 Replies

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

Answer by HawkSandwich · Jun 16, 2017 at 08:30 AM

There's no quick answer to this, and it'd even be really hard to estimate.

Vector3.Distance is a very, very costly algorithm to be calculating every frame for so many objects. This is mainly because it requires calculating square root. I can't tell you how many agents you'd be able to have, but I'll give you a little tip that might help avoid using Distance, which will allow you to have many many more agents.

Vector3.Distance really is for precise measurements, but if all you need is "is x within y distance of z?", then maybe you could just check those values manually. As an example, let's start with a one dimensional "version" of this. Let's say you have two cubes: the blue one is the player, the red one is the bad guy. If the red one is within a meter, have it kill the blue guy.

Using Distance, it'd look like this:

 Transform blueGuy, redGuy;
 void FixedUpdate () {
   if (Vector3.Distance(blueGuy, redGuy) < 1f) //kill the red guy
 }

Instead, we could do something like this:

 Transform blueGuy, redGuy;
 void FixedUpdate () { 
    if (Mathf.Abs(redGuy.transform.position.x-blueGuy.transform.position.x) < 1f) {
        //kill the blue guy
    }
 }

I'm using FixedUpdate here to reduce the amount of times we need to do the calculation, but of course it's still much less costly even when used in Update.

If you do that same calculation for x AND z (and maybe even y, if that's necessary), it'll save your device from the headache of trying to calculate square root every frame for potentially hundreds of objects.

If this isn't a possibility for you, definitely at least consider using your Vector3.Distance in FixedUpdate, that'll near double the amount of agents you can have by itself.

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 AarshSinghVishen · Jun 17, 2017 at 02:22 AM 0
Share

Thank you !

avatar image
0

Answer by lclemens · Mar 30, 2020 at 07:52 AM

Is there hard limit on the agent count? The Unity tower defense template uses a navmesh, but if you add over 100 agents, they all start grouping into bunches at nodes. It's as if the pathfinding can only be calculated for 100 or so at a time. FPS stays > 100 or so and cpu is < 20%. Agents are being created and recycled in a pool manager. I've spent an entire day looking for a max-agent-count setting somewhere in the code or in some unity setting, but I haven't been able to find it. Is this limitation in unity or is it some sort of hidden limit in the tower defense code?

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 lclemens · Apr 03, 2020 at 03:29 AM 0
Share

i think it's hitting a limit within navmesh. it seems that it takes a long time to calculate paths, so if you spawn 200 agents at the same time and tell them all to move to a specific spot, it'll take 20 to 30 seconds for unity to generate all the paths (it seems like maybe 3 or 4 per second) with no obstacles. I guess unity's built in navmesh was designed for games that need only a handful of agents.

avatar image UNDERHILL lclemens · Oct 19, 2020 at 04:42 PM 0
Share

Because it is clock sync'd and costly, it probably runs on 'complete or abandon', or 'complete or queue' logic, instead of 'hold frame to complete'.

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.

Follow this Question

Answers Answers and Comments

71 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 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 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

Make Navmesh agent follow closest object with tag 0 Answers

NavMeshAgent not crossing over certain tiles 0 Answers

Unity Navagation Ground Mapping 1 Answer

Precise distance calculation with high timeScale for NavMeshAgents? 1 Answer

Performance of coroutines in mobile 1 Answer


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