Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

Can Physics.IgnoreCollision disable OnTrigger... events?

Discussion in 'Physics' started by Teku-Studios, Dec 19, 2014.

  1. Teku-Studios

    Teku-Studios

    Joined:
    Sep 29, 2012
    Posts:
    257
    Here's my situation: I have a trigger that acts as "dark area" so that every sprite that enters it has its color light (V value - HSV) changed to give the player the feeling of entering a shadow zone. The trigger has a generic script that I use a lot throughout the game, and it affects every sprite inside the trigger.

    I use the OnTriggerStay() method because I want to smooth the sprites' color change depending on their Xpos (darker the closer they are to the centre of the trigger, and lighter the farther they are from it). Due to perspective issues, I want the main character to not send the OnTrigger Enter, Stay and Exit events just for one specific trigger, and I can't disable it since there is another character in it who does need to be endarked at te same time. I've tried using Physics.IgnoreCollision(trigger, player), but the trigger functionalities are still intact and working.

    Due to in-game limitations, I can't change the physics collision graph since that kind of trigger is a very specific one and modifying that now would break countless scenes. Is there some way I can achieve this without changing much code?


    *NOTE: Althoug it is a sprite-based game, it is a full 3D environment and we are working with PhysX, not Box2D, that's why I'm dealing with Physics instead of Physics2D.
     
    Last edited: Dec 19, 2014
  2. Teku-Studios

    Teku-Studios

    Joined:
    Sep 29, 2012
    Posts:
    257
    Never mind, I already solved this problem by setting a very simple flag on the shadow script configurable from the Inspector.

    Either way, is there a way to disable OnTrigger events somehow between 2 specific colliders with something as simple as Physics.IgnoreCollision()?
     
  3. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,355
    I know trigger events respect the collision layers, but I guess triggers don't respect the IgnoreCollision calls for specific objects.

    I wouldn't worry about it, though--trigger calls aren't super expensive, so unless you have hundreds of calls you want to avoid at the physics engine level a code/script solution is totally fine...
     
  4. Teku-Studios

    Teku-Studios

    Joined:
    Sep 29, 2012
    Posts:
    257
    Well, thank you for answering, but... I mean, I'm sorry but have you read the post? We're not concerned about performance whatsoever, we were explaining a very specific situation in which it could be useful to avoid OnTrigger events with a single line of code.

    Thanks for taking your time to post an answer, anyway.
     
  5. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,355
    Yes, I did. I guess my question is: Why try to avoid this at the physics engine level? Just convenience? Triggers have no effect in the physics simulation. By comparison, you can't cleanly undo the effects of a rigidbody collision in OnCollisionEnter. You can easily ignore the effects of OnTriggerEnter in your code, though.

    Structurally, it'd be easy to make a new component that provides optional dark zone settings, and then modify your generic trigger to check for it:

    Code (csharp):
    1.  
    2. function OnTriggerStay(Collider col)
    3. {
    4.     // or get the component off the current object, depending on your setup
    5.     var settings = col.GetComponent<DarkZoneSettings>();
    6.  
    7.     if(settings)
    8.     {
    9.         // modulate strength?  optional tint?
    10.  
    11.         // but also something like this to skip entirely
    12.         if(settings.disableEntirely)
    13.             return;
    14.     }
    15. }
    ...or whatever makes sense based on your current setup.

    (By the way--there is nontrivial overhead every time engine code calls into Mono, but it would only be an issue if you were getting thousands of callbacks every frame)
     
    Teku-Studios likes this.
  6. Teku-Studios

    Teku-Studios

    Joined:
    Sep 29, 2012
    Posts:
    257
    I simply added a bool and a 'If' check, and that solved it. The problem was that it is a generic script that we use throughout the game but we wanted that one to work slightly different without just making a new script with 99% the same code.

    What I did is add a bool called 'ignoreMainCharacter' set to false by default and, if true, the OnTriggerStay method checks if the colliding object's tag is 'Player' and then ignores it. Not exactly the best solution, but works there without breaking our setup for the entire game. Thanks for your approach!