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 /
  • Help Room /
avatar image
1
Question by LunaTrap · Mar 25, 2016 at 10:28 AM · 2d gamelistcollision detectionoverlappingai problems

2D How do i choose one out of two or three colliding gameobjects of the same type?

Hi guys i have a simple AI for my enemies that chase the player when they see them, let me show you my situation (see picture below please).

On situation 1the enemies chase the player and approach him, on situation 2, the enemies engage the player with melee attacks, on situation 3 the player moves away and on situation 4 the enemies follow the player, the problem is that the 2 enemies will stack up, and become one since they move at the same speed, since they all share the same script and the same float variable for the speed.

So my question is, how do i get a list or array of all enemies that are overlapping one another and pick one enemy at random from that list to increase its movement speed variable? this will have the effect that overtime they will separate, but i dont know how to acomplish this, please, any help is appreciated, i searched everywhere and no answer, thanks a lot!!

Edit: this should work even if not only 2 enemies overlapp, but also 3, 4, 5...

alt text

new-canvas.png (65.3 kB)
Comment
Add comment · Show 8
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 meat5000 ♦ · Mar 25, 2016 at 10:34 AM 1
Share

What have you tried?

avatar image LunaTrap meat5000 ♦ · Mar 25, 2016 at 09:00 PM 0
Share

its a 2d game, please see what i wrote below in repply to @justinijan

avatar image LunaTrap meat5000 ♦ · Mar 25, 2016 at 09:40 PM 0
Share

i require that when 2 enemies collide, only one of them gets its movement speed changed. but only one

avatar image LunaTrap meat5000 ♦ · Mar 26, 2016 at 12:09 AM 0
Share
 public class Enemy
 {
     public bool hasCollided = false;
     Collider2D col;
 
     private void OnTriggerEnter2D(Collider2D other)
     {
         if (other.transform.CompareTag("Enemy"))
         {
             Enemy script = other.transform.GetComponent<Enemy>();
             if (!script.hasCollided)
             {
                 col = other;
                 script.hasCollided = true;
                 Debug.Log("Collision" + script.hasCollided + hasCollided);
             }
         }
     }
 
     private void OnTriggerExit2D(Collider2D other)
     {
         if (other == col)
         {
             hasCollided = false;
         }
     }
 }

this script wite to the console each time it collides with another enemy, i set a variable on one of the enemies triying to make the other enemy unable to detect collision also

but event still

this is the output in the console

alt text

This is what i need, that when 2 enemies collide, the code only get triggered in one of the enemies.

ffsddr.jpg (10.9 kB)
Show more comments
avatar image Miroske · Mar 25, 2016 at 11:05 AM 0
Share

I think that a more efficient solution would be to add navmesh agent components to enemies, the agents can't overlap and the built in path finding and avoidance systems will spare you of many potential problems you can have with your AI system in the future.

As for putting all enemies in an array there are many ways to do that depending on the structure of your project and enemy controller scripts. For example you can create a script in which you declare a public array and then assign the values in the inspector. Then you can do something like:

 for (var i = 0; i < enemiesArray.Length; i++)
 {
         //Set random speed values to every enemy using Random.Range().
 }
 

As for picking one random element of an array or list it is usually done using shuffle algorithms, but u can just select a random number using Random.Range(0, enemiesArray.Length) and pass that number as an array index.

avatar image LunaTrap Miroske · Mar 25, 2016 at 08:59 PM 0
Share

Its a 2d game, so, no navmesh, i know how to picka random item froma list or array, but the problem is that i need to get a list of all gameobjects collising with each other, once i have a list, i know how to choose a random item

the problem is that if i try to get a list of colliding gameobjects using Physics2D.OverlapSphere when 2 enemies collide, and then i choose one of the items from the list that Physics2D.OverlapSphere returns to change the movement speed of the chosen enemy, this will happen also in the other enemy script, and the effects will be nullified

since the collision and enemy choosing code will execute at the same time on each enemy's script, what i need is to have the code executet only on one of the enemy's script and only once

let me put a simplier example, when 2 enemies collide, i require that one of those 2 enemies get chosen at random, this is what i need, so when this enemy gets selected i will execute the Physics2D.OverlapSphere on that enemy script only

avatar image LunaTrap Miroske · Mar 25, 2016 at 09:39 PM 0
Share

maybe another example

i require that when 2 enemies collide, only one of them gets its movement speed changed. but only one

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Ali-hatem · Mar 26, 2016 at 02:21 PM

why don't you use random number for speed when enemyes collide with each other that last for secondes & then back to normal speed & don't worry if they use the same script the resault will be different for each object i have already tested it so cheek this example :

 public int speed = 5;
 public int RandomSpeed;

 void OnTriggerEnter2D(Collider2D other){
     if (other.tag == "Enimy") {
         print ("stay out of my way");
         StartCoroutine (changeSpeed());
     }
 }

 IEnumerator changeSpeed(){
     RandomSpeed = Random.Range(1,5);
     speed = RandomSpeed;
     yield return new WaitForSeconds (3);
     speed = 5;
 }
Comment
Add comment · Show 4 · 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 LunaTrap · Mar 26, 2016 at 08:43 PM 0
Share

Thats an easy solution, but the problem is that there exist the possiblity that both enemies will get a very similar speed or the same speed, i want this to enver happen

but thanks a lot for your solution, i will keep it as plan B if im not able to find a solution that is 100% sure that not enemies will have the same speed

avatar image Ali-hatem LunaTrap · Mar 27, 2016 at 09:54 AM 0
Share

if by chance they get the same speed that means they will remain collided & as we tested if they collided will keep generate random speed.

avatar image LunaTrap Ali-hatem · Mar 27, 2016 at 11:35 PM 0
Share

i guess it could be useful to after changing the speed, check to see if the speeds are within a range, and if both speeds are too smilar, get a new random speed for both

thnkas for you help :)

Show more comments
avatar image
1

Answer by kcather · Mar 26, 2016 at 07:40 PM

"i managed to make the code only trigger on one of the enemies on collision, what i had to do, is to change the hasCollided varaible to true in the local script instead of triying to change the veraible to true in the other enemy."

Hey Luna, it's Kenny. This is just a thought and I could be wrong.

I believe the reason you had to change it in the local script instead of the enemy is you've already initialized the enemy object. You're not changing a property of the enemy, you're just changing how fast the enemy moves in the room. So, it might be advantageous to make it a room property.

Or somewhere other than in the enemy class because you're running into the problem you described of changing it for all the enemies at once.

I dunno...we can try a few different approaches if you want.

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 LunaTrap · Mar 26, 2016 at 08:47 PM 0
Share

yes, that was my thinking when i tried doing it the other way around, and it worked

i have yet not solid answer, but im still working in finding one @Ali hatem guggested a simple solution, but before i give up and go with that one, i want to research more

:)

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

43 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

Related Questions

Detecting missing collision in 2D platformer to change direction for enemy - without Raycast 1 Answer

Player passenger moving when being pushed by two platforms 0 Answers

Transform.position in 2d game or better way to move player working with colliders. 1 Answer

Snake like movement in 2D space 0 Answers

Issues with 2D collision/overlap detection,Help detecting 2D collision/overlap 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