Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug OnTriggerEnter2D triggered despite being ignored in the layer collision matrix.

Discussion in 'Physics' started by DearUnityPleaseAddSerializableDictionaries, Jan 7, 2023.

  1. DearUnityPleaseAddSerializableDictionaries

    DearUnityPleaseAddSerializableDictionaries

    Joined:
    Sep 12, 2014
    Posts:
    135
    Code:

    Code (CSharp):
    1.  
    2.     protected virtual void OnTriggerEnter2D(Collider2D collision) {
    3.         Debug.Log($"Test: {Physics2D.GetIgnoreLayerCollision(gameObject.layer, collision.gameObject.layer)} & {Physics2D.GetIgnoreLayerCollision(collision.gameObject.layer, gameObject.layer)}.");
    4.     }
    5.  
    This sometimes outputs Test: True & True, and is triggered between two objects of layers that I've made sure to uncheck in the layer collision matrix.

    I'm unable to reproduce this on a fresh project or scene. Are there certain conditions where they're triggered despite unchecking it in the Physics2D layer collision matrix?

    Unity version: 2021.3.16f1

    **SOLVED in Post #8**: https://forum.unity.com/threads/ont...-layer-collision-matrix.1383468/#post-8714295
     
    Last edited: Jan 29, 2023
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,513
    The layers are checked when two things come into contact. If it says they cannot, a contact is not created and therefore nothing will happen which obviously includes subsequent callbacks.

    If you're changing the setting in script while callbacks are happening, it won't change callbacks yet to happen as they all happen together at the end of the simulation step. Same if you were to change the layer during callbacks. Changing this ensures that new contacts will behave as you've requested and existing contacts will be adjusted to be allowed or not allowed. This won't affect callbacks that are being performed from the simulation step.

    Beyond that, there's no reason and this has never been reported.

    NOTE: You only need to check one direction here as it'll automatically be a bi-direction check of layer vs layer.
     
  3. DearUnityPleaseAddSerializableDictionaries

    DearUnityPleaseAddSerializableDictionaries

    Joined:
    Sep 12, 2014
    Posts:
    135
    Thank you for the help + info.

    I never called `Physics2D.IgnoreLayerCollision` (I checked the references in Visual Studio too).

    I finally managed to reproduce it in a new scene with new gameobjects/scripts but not sure if it's WAI.

    So I have an object called "Parent" which has a child called "Child", in layers A & B respectively, where A cannot interact with B according to the matrix. If I duplicate this object, then Parent interacts with the other parent's child, despite being in separate layers that should not interact with each other. Both parent and child have a "isTrigger" 2D collider component. Parent has a Rigidbody2D component.

    For now, after diagnosing this, I've worked around it so this is not affecting me and I don't know if this is a bug or WAI.
     
    Last edited: Jan 7, 2023
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,513
    Sorry, the descripton is a tad confusing. Know that parent child relationships are of no concern to physics, it never sees them. It is simply concerned about when two colliders potentially overlap, it then checks their layer masks to see if they can. No transform hierarchies are involved and it's super simple what happens and it doesn't involve anything to do with Unity (the Box2D physics system) in reality.

    In terms of callbacks after the simulation has run; when two colliders come into contact, a callback happens on the Collider2D GameObject as well as the Rigidbody2D GameObject if it's not on the same GameObject (parent) and the same happens for the other Collider2D/Rigidbody2D involved in the contact.
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,513
    If you have a simple reproduction project then please feel free to share it and I can take a look for you.
     
  6. DearUnityPleaseAddSerializableDictionaries

    DearUnityPleaseAddSerializableDictionaries

    Joined:
    Sep 12, 2014
    Posts:
    135
    Here is the bug report - I've attached the project and script.

    https://unity3d.atlassian.net/servicedesk/customer/portal/2/IN-28144

    Script:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Example : MonoBehaviour
    6. {
    7.     protected virtual void OnTriggerEnter2D(Collider2D collision) {
    8.         Debug.Log($"{name} collided with {collision.name}." +
    9.             $"Test: {Physics2D.GetIgnoreLayerCollision(gameObject.layer, collision.gameObject.layer)} & {Physics2D.GetIgnoreLayerCollision(collision.gameObject.layer, gameObject.layer)}.");
    10.     }
    11. }
    12.  
    Parent has that script + RigidBody2D (Kinematic) and Collider2D (Trigger), child has same except no Rigidbody2D. Parent is in a layer that shouldn't be able to collide with the child's layer.

    Then I duplicated that parent, prefixed the names with "Other" for that duplicated object, then this is one of the outputs:


    OtherParent collided with Child.Test: True & True.
    UnityEngine.Debug:Log (object)
    Example:OnTriggerEnter2D (UnityEngine.Collider2D) (at Assets/Example.cs:8)
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,513
    Unfortunately I cannot access a bug report until QA have processed it or at least until they are around next week. I just wanted access to the test project really. Regardless, thanks for the instructions, I'll give this a go setting up shortly.

    It wouldn't anyway. The child collider is attached to the Rigidbody2D. Sibling colliders (colliders attached to the same Rigidbody2D) cannot ever come into contact with each other. Different Rigidbody2D can of course. That said, you're debugging the check which says if those layers are allowed to come into contact which isn't a statement of whether the colliders in question could ever be allowed to contact; those two things are not the same.

    So you must've set these Rigidbody2D to use full kinematic contacts. If not, you're contacting something else. Kinematic/Kinematic do not come into contact unless you ask for it.
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,513
    Okay so I just created a test project following your instructions and I too get that callback but I get it because the two child colliders are on Layer "B" and following your instructions, only Layer "A" and Layer "B" cannot come into contact. This is correct behaviour.

    However, if you place this callback on the Rigidbody2D (the collider there didn't collide) then using "gameObject.layer" isn't anything to do with the two colliders coming into contact.

    See what I said above:
    Unfortunately, because trigger callbacks only tell you the "other" collider, you have no way of knowing which one on "this" side actually collided. You've made that even worse in your case because you're assuming it's the one on the "Parent" GameObject which is likely isn't.

    Solution:
    Change it to not be a trigger and use the "Collision2D" structure instead, that gives you both sides and because it's Kinematic, you won't get a collision response.

    Using the following method on a script on the "Parent" GameObject now correctly produces "false" as the output:
    Code (CSharp):
    1.     void OnCollisionEnter2D(Collision2D collision)
    2.     {
    3.         Debug.Log($"Test: {Physics2D.GetIgnoreLayerCollision(collision.collider.gameObject.layer, collision.gameObject.layer)}.");
    4.     }
    5.  
    Also, would you please request that the bug case be closed?
     
  9. DearUnityPleaseAddSerializableDictionaries

    DearUnityPleaseAddSerializableDictionaries

    Joined:
    Sep 12, 2014
    Posts:
    135
    Ahhh! Thank you very much for the detailed explanation and clearing up my confusion - it really confused me but it makes sense now. I have replied to the bug for it to be closed (I fiddled around with the Atlassian UI and there wasn't an option for me to close the bug).
     
    MelvMay likes this.