Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by firfur · Jun 02, 2021 at 01:33 PM · c#unity 5navmeshagent

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;
     }
 }
Comment
Add comment · Show 4
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 Janette5 · Jun 04, 2021 at 10:01 AM 1
Share

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.

avatar image firfur Janette5 · Jun 04, 2021 at 11:25 AM 0
Share

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 :)

avatar image Janette5 firfur · Jun 04, 2021 at 01:56 PM 1
Share

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

Show more comments

1 Reply

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

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.alt text


screenshot-2021-06-04-182940.png (177.8 kB)
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.

Follow this Question

Answers Answers and Comments

215 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 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 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 avatar image avatar image

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


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