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 /
  • Help Room /
avatar image
0
Question by In0sc0p3dJFK · Jul 09, 2021 at 05:02 AM · inputainavmeshnavmeshagentnavigation

=> How to Avoid NavMeshAgents Clumping Up Together?! <=

Hey so i'm using NavMeshAgents and i'm moving them around by clicking on a place in the world, and all they do is move there. That simple. How can I avoid the agents clumping together?:


https://drive.google.com/file/d/1OF0ax7V8NfVCV9JS15w60jIdDaGP92oR/view?usp=sharing


As you can see from the video attached above, just regularly moving one agent to a specific spot that I click is working just fine. But when assigning multiple agents to a spot, they all appear to fight eachother for the spot and never reach their destination. This also means that their navmesh velocity will always be over a vector zero. Which is really important because it can be useful for animation, and other functionality.


What I've Tried:

How can I make them not clump up like this? I've already tried:

  • not detecting collision between eachother.


  • I've tried putting navmesh obstacles on each of the troop (agents)


  • ive tried creating an overlap/checksphere on the position of my mouse click and checking how many agents have entered the list.


  • I've tried checking if their current velocity is under a current number.


This is my unit movement script:

 public class UnitMovement : MonoBehaviour
 {
 
     Camera cam;
 
     NavMeshAgent nav;
 
     public LayerMask ground;
 
     void Start()
     {
 
         cam = Camera.main;
         nav = GetComponent<NavMeshAgent>();
 
     }
 
     void Update()
     {
 
         if (Input.GetMouseButtonDown(1))
         {
 
             RaycastHit hit;
             Ray ray = cam.ScreenPointToRay(Input.mousePosition);
 
             if (Physics.Raycast(ray, out hit, Mathf.Infinity, ground))
             {
 
                 nav.SetDestination(hit.point);
 
             }
 
         }
 
     }
 
 }



I'm not sure what to do. Please any help!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DenisIsDenis · Jul 10, 2021 at 05:54 AM

I have modified your script. In it, I announced the list of agents. The first agent on the list goes to the click point, and the rest line up around it. If you have questions about the script, ask.

 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.AI;
 
 public class UnitMovement : MonoBehaviour
 {
     Camera cam;
     NavMeshAgent nav;
     public LayerMask ground;
     public static List<NavMeshAgent> meshAgents = new List<NavMeshAgent>();
 
     void Start()
     {
         cam = Camera.main;
         nav = GetComponent<NavMeshAgent>();
         meshAgents.Add(nav);
     }
 
     void Update()
     {
         if (Input.GetMouseButtonDown(1) && meshAgents.Contains(nav))
         {
             if (meshAgents.IndexOf(nav) == 0)
             {
                 RaycastHit hit;
                 Ray ray = cam.ScreenPointToRay(Input.mousePosition);
 
                 if (Physics.Raycast(ray, out hit, Mathf.Infinity, ground))
                 {
                     nav.SetDestination(hit.point);
                 }
 
                 float angle = 60; // angular step
                 int countOnCircle = (int)(360 / angle); // max number in one round
                 int count = meshAgents.Count; // number of agents
                 float step = 1; // circle number
                 int i = 1; // agent serial number
                 float randomizeAngle = Random.Range(0, angle);
                 while (count > 1)
                 {
                     var vec = Vector3.forward;
                     vec = Quaternion.Euler(0, angle * (countOnCircle - 1) + randomizeAngle, 0) * vec;
                     meshAgents[i].SetDestination(nav.destination + vec * (nav.radius + meshAgents[i].radius + 0.5f) * step);
                     countOnCircle--;
                     count--;
                     i++;
                     if (countOnCircle == 0)
                     {
                         if (step != 3 && step != 4 && step < 6 || step == 10) { angle /= 2f; }
                         
                         countOnCircle = (int)(360 / angle);
                         step++;
                         randomizeAngle = Random.Range(0, angle);
                     }
                 }
             }
         }
     }
 }

Just modify the list meshAgents to manage different groups of agents.

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

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

Farthest Reachable Destination 0 Answers

Second NavMesh Surface refuses to cooperate: Failed to create agent because it is not close enough to the NavMesh 0 Answers

ai patrol and chase question 0 Answers

NavMeshLink don't work in correctly in certain area 0 Answers

NavMeshSurface or NavMeshAgent agentTypeId can't be changed on runtime 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