Search Unity

Multiple MeshColliders as Trigger-Conjunction to detect other objects

Discussion in 'Physics' started by AutoAssault, Sep 9, 2019.

  1. AutoAssault

    AutoAssault

    Joined:
    Apr 17, 2019
    Posts:
    7
    Hello

    I am trying to detect objects within an arc segment-shaped collider consisting of two mesh colliders (see picture below). It is in 3D, so both colliders have a height of some unity units.
    upload_2019-9-9_2-14-37.png
    The reason I am using two colliders here is that I'd like to have this "arc segment" shape which is not a default collider shape in Unity. This lets me adjust the angle and the overall size of the arc segment.

    The goal is to detect objects within the green hatched area. The colliders are setup within the following GameObject structure in Unity:
    • ParentObject (with Rigidbody & script to control childs --> active / inactive)
      • ChildObject_1 with Cylinder Collider & script which has OnTriggerEnter()
      • ChildObject_2 with Triangle Collider & script which has OnTriggerEnter()

    Both child GameObjects are set to inactive as default. If the player presses a button, both childs get activated from the parent script and they detect all objects with a collider if they are inside. This is working.

    In order to achieve the AND-Junction (overlapping) of both colliders, I'd like to store all collisions (as references) to compare them later in the parent script. If an object is detected on both colliders, I know the object is within the hatched are, if not, I can ignore them.

    For each object within the colliders, the OnTriggerEvent() function is fired, and I can add them to a list. This is working as well.

    But here come my questions:
    1. How do I know, when both colliders have finished detecting all objects? For example, if there are 3 objects in the hatched are, how do I know when have detecte all 3 objects? I've run some tests, and noticed that if I check the List right after I set both colliders to active, the list sometimes returns no elements, which means that I checked the list too early or in other words: the OnTriggerEnter() functions have not been executed yet.
    2. I read that OnTriggerEnter is executed in FixedUpdate(), but my parent script sets both colliders active and inactive in the Update() function. Is this a problem?
    I appreciate any adivces, thanks :)
     
  2. AutoAssault

    AutoAssault

    Joined:
    Apr 17, 2019
    Posts:
    7
    No one any ideas on this?
     
  3. jnbbender

    jnbbender

    Joined:
    May 25, 2017
    Posts:
    284
    Instead of querying (parent checking children's detection's), trying reporting up to the parent when an object is detected in the OnTriggerEnter(). So in each colliders OnTriggerEnter(), have it register the object with the parented object. This way, you're not worried about checking your List too early. Let the parent filter the duplicate collisions. I think this is what you want?