Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Multiple Collider detection and OnTriggerEnter calls on the same Collider

Discussion in 'Scripting' started by Emolk, Oct 10, 2019.

  1. Emolk

    Emolk

    Joined:
    Feb 11, 2014
    Posts:
    241
    When the Player shoots, a 'missile' is fired. When this missile explodes (on contact), it spawns an explosion object at its location. This explosion object is picking up multiple OnTriggerEnter2D calls on enemies, which have only one collider on them. As well as this, Physics2D.OverlapCircleAll returns the SAME collider on the enemy multiple times. Here is the necessary code:

    Explosion : AttackClass

    Code (CSharp):
    1. void Start()
    2. {
    3.  
    4.     Colliders =
    5.     Physics2D.OverlapCircleAll(gameObject.transform.position,
    6.     0.5f);
    7.  
    8.     // 0.5f is the radius of the collider
    9.  
    10.         foreach (var Collider in Colliders)
    11.         {
    12.             if(Collider.tag == "Enemy"){
    13.                 print("Hit enemy");
    14.             }
    15.         }
    16. }
    17.  
    18. public override void OnTriggerEnter2D(Collider2D col){
    19.         if (col.tag == "Enemy"){
    20.             print("Triggered");
    21.         }
    22. }
    Most of the time it only prints each statement once, however when the Player is touching the Enemy or sometimes when the Player is below the Enemy and fires a missle, each print statement is printed twice and on some enemies (bigger ones) even three times. There is only one enemy in the scene. I don't really have any idea how this is happening, but maybe it has something to do with the colliders contact points?

    Zombie Enemy:



    Slime enemy:



    Explosion object:



    As you can see my enemies only have one capsule collider on them each, which is triggering OnTriggerEnter multiple times as well as returning multiple times in Physics2D.OverlapCircleAll.

    On the slime enemy it would trigger OnTriggerEnter / return multiple times in Physics2D.OverlapCircleAll one time normally and 2 times abnormally (when the Player is positioned extremely close to the enemy, or when the attack comes at a weird angle)

    On the zombie enemy it is one time normally and 2-3 times abnormally. I think this is because the zombie enemy has a larger collider?

    This issue has been plaguing my development for a while now and i have made a couple of threads on this with no responses.

    I hope i have outlined the issue with enough detail and i would very much appreciate any type of response.

    Thanks.
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,385
    Never heard of such a thing. Are you sure that you have only one AttackClass running? Try changing your print to something like:
    Code (csharp):
    1.  
    2. print(GetInstanceID()+ " is colliding with "+ col.gameObject.GetInstanceID());
    3.  
    Also, you have a variable called Collider, which is also the name of a Unity-defined class. You probably want to change that name.
     
    Emolk likes this.
  3. Emolk

    Emolk

    Joined:
    Feb 11, 2014
    Posts:
    241
    Wow this is it. Two explosions are being spawned however i have no idea why.

    Code (CSharp):
    1. public class Karma : AttackClass
    2. {
    3.     public GameObject explosion;
    4.     public float lifeTime;
    5.     void Start()
    6.     {
    7.         lifeTime = 0.5f;
    8.         Invoke("Death", lifeTime);
    9.     }
    10.  
    11.     void Death(){
    12.         GameObject objectInstance = Instantiate(explosion,
    13.         gameObject.transform.position, Quaternion.Euler(new Vector3(0, 0, 0)));
    14.         print("Spawning explosion");
    15.         Destroy(gameObject);
    16.     }
    17.  
    18.     override public void OnTriggerEnter2D(Collider2D col){
    19.         Death();
    20.     }
    21. }
    This is the code that spawns the explosion. It should only spawn once every time right? I don't see any situation where it could spawn two explosions. Only one Karma is being spawned as well.
     
  4. TheGonzoGamer

    TheGonzoGamer

    Joined:
    Apr 24, 2019
    Posts:
    35
    This suggestion helped me troubleshoot, WHAT I THOUGHT, was an OnTriggerEnter registering multiple hits when, on closer inspection of the InstanceID, it was the OnTriggerEnter from the object in the next scene being loaded on top of the player's current position.

    I owe you a beer my good man.