Search Unity

OnTriggerEnter2D does not work

Discussion in '2D' started by Ritmac, Jul 20, 2019.

  1. Ritmac

    Ritmac

    Joined:
    Jul 20, 2019
    Posts:
    4
    I have an object with several colliders, most of them are triggers, when a player is in a certain zone, the golem attacks and must register for the trigger, but it registers to enter any of the colliders.
    The object has four children, the script with OnTriggerEnter2D is located on the main object, that is, in theory, it would be necessary to use only its colliders, but the method uses in addition the children colliders.
     

    Attached Files:

    • sa.png
      sa.png
      File size:
      642.8 KB
      Views:
      606
  2. Ritmac

    Ritmac

    Joined:
    Jul 20, 2019
    Posts:
    4
    So, I found that if you remove Rigidbody 2D from the golem, then the script and colliders work correctly, what's the problem then?
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    It seems you're not using Layers to filter what can collide with what using the Collision Matrix in the Physics 2D settings. If you're letting everything contact everything but then filtering stuff by checking tags (which is much slower) then perhaps there's some logic error in your callback.

    I'm only guessing. If the problem is easy to duplicate and you don't have a problem doing so, zip-up your project and upload it here and I can take a quick look. I'll delete it after I have a copy. Also add a quick description on how to duplicate the issue.
     
  4. Ritmac

    Ritmac

    Joined:
    Jul 20, 2019
    Posts:
    4
    Thanks for the answer, I did not quite understand what is written in the documentation. In search of an answer, I looked there, in the section on colladers and Rigidbody, it was written there that if a collader does not have a Rigidbody, then the correct calculation of collision physics does not occur.
    Surprisingly, after adding Rigidbody to the objects, it all worked, but I did not understand why this works.
    I'm not sure that this is correct from the side of performance, since it will be necessary to calculate body physics for these objects additionally, even though they should just follow the parent object
    I can send a project if it is still necessary, did not send, because the issue is potentially resolved
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    No, that's not what it says although it can be confusing. If you don't add a Rigidbody then the Collider(s) are static i.e. non-moving; something you'd use for static geometry i.e. level platforms, floors, walls etc.

    Static colliders don't contact static colliders which makes sense as they are not supposed to be moved and this is probably what you're thinking of. Dynamic colliders will contact static colliders which is why you'll often see people on the forums saying you'll need to add at least a single Rigidbody to a collider which is just plain confusing and lacks the description of why.

    There's a table of which type of colliders contacts other types of colliders i.e. dynamic, kinematic and static in the manual. Also, changing whether they're a trigger or not can slightly modify this.

    Hope this helps.
     
    Last edited: Jul 31, 2019
  6. Ritmac

    Ritmac

    Joined:
    Jul 20, 2019
    Posts:
    4
    Yes, it helped a little, thanks, I'll sit and read about what you wrote.
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    Okay cool, good luck with it.
     
    Ritmac likes this.