Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 InfixionGames · Mar 02, 2019 at 11:12 PM · scripting problemaiprogramming

Stop enemies from grouping up

In my game I have multiple enemies. These enemies move towards the player. However if the player circles around the enemies the enemies group up and eventually you only see one enemy. How can I keep this from happening?

Thank you in advance.

The code: enemyHealthBar.fillAmount = antHealth / antHealthBarDivide; float distance = Vector2.Distance(target.position, transform.position);

         if (distance < detectionRange)
         {
  
 
             if (Vector2.Distance(transform.position, target.position) > 1)
             {
                 transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
 
 
             }
 
             Vector2 direction = target.position - transform.position;
             float angle = Mathf.Atan2(direction.x, direction.y) * Mathf.Rad2Deg;
             Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.back);
             transform.rotation = Quaternion.Slerp(transform.rotation, rotation, rotationSpeed * Time.deltaTime);
             
         }
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
3
Best Answer

Answer by Ymrasu · Mar 03, 2019 at 12:40 AM

Should look into group steering behaviors. What you are looking for in your question is a separating behavior. A simple way can be done by casting a sphere around each enemy to look for other enemies. Then have each enemy move away from the others. I put together something simple that you can put directly after the code you provided:

 float separateSpeed = speed/2f;
 float separateRadius = 1f;
 
 Vector2 sum = Vector2.zero;
 float count = 0f;
 
 // overlapshere to detect others
 var hits = Physics.OverlapSphere(transform.position, separateRadius);
 foreach(var hit in hits) {
     // make sure it is a fellow enemy ** use your enemy script name **
     if(hit.GetComponent<Enemy>() != null && hit.transform != transform) {
         // get the difference so you know which way to go
         Vector2 difference = transform.position - hit.transform.position;
 
         // weight by distance so being closer means moving more
         difference = difference.normalized / Mathf.Abs(difference.magnitude); 
 
         // add together to get average of the group
         // this allows those at the edges of a group to move out while
         // the enemies in the center of a group to not move much
         sum += difference;
         count++;
     }
 }
 
 if (count > 0) {
     // average the direction
     sum /= count;
             
     // set the speed of movement
     sum = sum.normalized * separateSpeed;
 
     // this is where you would apply this vector for movement
     // i am basing this off of the code you provided
     transform.position = Vector2.MoveTowards(transform.position, transform.position + (Vector3)sum, separateSpeed * Time.deltaTime);
 }
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 InfixionGames · Mar 03, 2019 at 10:58 AM 0
Share

Thank you for the quick response, it works now. One point to note is that I didn't specify that it was a 2d game so I had to change

  var hits = Physics.OverlapSphere(transform.position, separateRadius);

to:

             var hits = Physics2D.OverlapCircleAll(transform.position, separateRadius);

Thanks again.

avatar image fivegrandpint · Mar 30 at 10:14 AM 0
Share

I know this is a topic from a long time ago, but I am looking to achieve a similar effect here and am looking to adapt this code. I am assuming that the "count" variable refers to the number of enemies in the group? What I am confused about is how this is calculated and what the purpose of the " count++; " part is? Does this not just add 1 to the count every time? Is this script running in the update function, if so, wouldn't that just add 1 every frame?

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

238 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

Related Questions

Multiple Cars not working 1 Answer

(AR) How to run 3D image (.obj) inside AR using external server (namecheap vps) and assign it to parent meaning that the 3D image is under image target? 0 Answers

OnTriggerEnter called only one time?!! 5 Answers

More Efficient way? 1 Answer

How do I make it so that a walking animation plays for the enemy AT THE SAME TIME it is moving toward the player? 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