Search Unity

  1. How can our website serve you better? Give us your feedback. Take our survey and let us know.
    Dismiss Notice

Clear ignored collisions set in Physic.IgnoreCollsion

Discussion in 'Scripting' started by Compressed, Aug 10, 2015.

  1. Compressed

    Compressed

    Joined:
    Sep 20, 2014
    Posts:
    59
    I have a collider and with Physic.IgnoreCollsion i set this collider to ignore collisions with other colliders so
    Code (CSharp):
    1. Physic.IgnoreCollsion(collider, collider1);
    2. Physic.IgnoreCollsion(collider, collider2);
    3. Physic.IgnoreCollsion(collider, collider3);
    But after some time i need to clear all the ignored collisions i have set, so something like Physic.ClearIgnoredCollisions(collider);

    I do not have a list (nor do i want to keep one) of collisions which this collider is set to ignore so i cannot do this Physic.IgnoreCollsion(collider, collider1..2..3 etc, false);

    So can i erase the set ignored collisions from the collider or pull up the list of colliders it ignores, or something like that?

    thanks
     
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Just deactivate and reactivate the collider. IgnoreCollisions in general feels really hack-ish to me though, and heavy usage feels like you may have a more fundamental issue. If you want a more easily controllable, easily reversible, and persistent method for controlling collisions, you really need to do it using layers and not specific exceptions. I don't know the specifics of your situation, but the following two options are easily overlooked:
    1. Have the main character/vehicle/whatever have 2 main layers, one that interacts with most other objects in the scene and one that interacts with very few. Both would be set up beforehand, and using a simple state machine you'd change your char from one to the other and back again as needed. You could also do this with projectiles and other objects if needed, but as that makes logic and management far more difficult, I usually stick with the main character-only.
    2. Set up a compound collider on the objects that need it. This is accomplished by giving them an empty child object and attaching a new collider to that child. Any collisions with the child will trigger the same functions as collisions with the parent collider, but you can activate/deactive the child collider at will- it can have a different shape of course if you want, but most importantly it can be on a completely different layer. Using this, your object can essentially be on multiple layers simultaneously, which will solve collision issues by making the child layer one that interacts with some other objects (which you only need temporary interaction with), and disabling the child and/or its collider when it's not needed.
     
unityunity