Search Unity

Question Clarification on how multiple trigger-colliders/parent and child trigger-collider relationship

Discussion in 'Scripting' started by Inverzus, Jul 18, 2020.

  1. Inverzus

    Inverzus

    Joined:
    Jun 26, 2020
    Posts:
    12
    So I'm trying program some simple AI for my 2D Top-Down space fighter game. Right now, I have a Circle Collider 2D attached to the enemy ship that extends in a set radius that I'm using to detect potential targets to lock-on onto. I also thinking of making a small trigger collider that detects very nearby objects, thus signaling to the AI that it should attempt to avoid/take evasive maneuvers.

    So from my understanding of colliders, that since I have the lock-on trigger-collider on the parent if I attach the proposed proximity trigger-collider to a child object, it would count as part of the parent collider, thus it would trigger my lock-on functions even though I would not want it to. The solution to this problem from what I can tell online, is to either give the child object also a kinematic rigidbody2D, which I personally feel is a bit wasteful, or to have both triggers, the lock-on, and the proximity, as a child of the enemy ship, thus they would not "trigger" each other?

    Is my understanding correct? What would be considered a better solution?
     
  2. AnthonySharp

    AnthonySharp

    Joined:
    Apr 11, 2017
    Posts:
    88
    Personally I would do it in script rather than use colliders. For example:

    Code (CSharp):
    1. public void ScanArea(float scandistance)
    2.     {
    3.         GameObject[] gos = GameObject.FindGameObjectsWithTag("EnemyShip");
    4.  
    5.         foreach(GameObject g in gos)
    6.         {
    7.             if (Vector3.Distance(transform.position, g.transform.position) <= scandistance)
    8.             {
    9.                 // lock onto that ship
    10.             }
    11.         }
    12.     }
     
  3. Inverzus

    Inverzus

    Joined:
    Jun 26, 2020
    Posts:
    12
    What are the advantages of doing this in scrip rather than colliders?
     
  4. AnthonySharp

    AnthonySharp

    Joined:
    Apr 11, 2017
    Posts:
    88
    I would say there are a few:

    • Probably faster computation-wise (but I don't know too much about collision algorithms). But in practice code will almost certainly be faster because you can run the ScanArea() function as (in)frequently as you like, whereas colliders are always active. Running ScanArea() once a second is much faster than having 100 colliders constantly going IMO. If you want to make it even faster you can pool the ship objects, rather than calling FindGameObjectsByTag every time.
    • You don't run into your issue with colliders triggering themselves, because there are no colliders to worry about :p
    • More customisable - you can have just one function that scans for space ships and just adjust the scandistance float in the function. So you can for example have different viewing distances for different ships.
    • Colliders can be unreliable if it gets laggy etc. Script will always have your back because it doesn't rely on dodgy physics engines to tell if there's an enemy nearby.