Search Unity

Detecting collisions at one precise time

Discussion in 'Physics' started by LeRan, Feb 22, 2017.

  1. LeRan

    LeRan

    Joined:
    Nov 24, 2015
    Posts:
    118
    Hello forum, I'm not totally sure if my question falls into the "Physics" directory but here it is.

    I need to know what objects are currently colliding with one specific trigger object at one specific time.

    I don't need to have behaviours happen when that trigger object enters or exits collisions : I just need to have the list of things it's actually colliding with at the moment I call one specific script. I would love to write something like :
    GameObject[] theCollidingObjects = myCollider.listofCurrentCollisions
    but unfortunately it does not work like this.

    Is there a simple solution to do that ? Or do I have to write OnCollisionEnter and OnCollisionExit functions, where objects log themselves in and out of a log book everytime they enter or exit, so that I can have their list when I need it ? It sounds like an awful waste of CPU resource...
     
  2. LeRan

    LeRan

    Joined:
    Nov 24, 2015
    Posts:
    118
    Well that's what I finally did : all interesting objects in the scene have a OnTriggerEnter and OnTriggerLeave procedure, where they register in and out of a logbook everytime they cross the trigger object's path, so that when I need it I just have to read the logbook to see who's here.

    It still feels like an awful waste of computing time though.
     
  3. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
  4. p1zzaman

    p1zzaman

    Joined:
    Jan 1, 2017
    Posts:
    64
    Do you require a handle to the gameobject being logged? also if the gameobject is destroyed after it was logged how will you manage this collection?
     
    LeRan likes this.
  5. LeRan

    LeRan

    Joined:
    Nov 24, 2015
    Posts:
    118
    This sounds like the solution I'm looking for, but for some reason I can't make it work.

    I have my "interesting" objects with one rigidbody and sphere collider each, and my "trigger" object with one box collider (in "trigger" mode) and one rigid body (that I added because SweepTestAll needs it).

    But when I SweepTest the trigger's object Rigidbody, the code always returns a void array, even though I make sure that there is at least one "interesting" object inside. I suppose that I'm doing something wrong, but what ?

    RaycastHit[] stuffInside = myTriggerRigidbody.SweepTestAll (Vector3.one, 0.01f, QueryTriggerInteraction.Collide);

    And I always get : stuffInside.Length == 0

    (I don't need the trigger object ta actually sweep, but the result is exactly the same when I set the test the Vector3.zero and maxDistance = 0)
     
  6. LeRan

    LeRan

    Joined:
    Nov 24, 2015
    Posts:
    118
    Edy likes this.