Search Unity

Question Collisions Detected Between Layers When Collisions Between Layers Are Disabled Via Layer Matrix

Discussion in 'Editor & General Support' started by CTV123, May 26, 2023.

  1. CTV123

    CTV123

    Joined:
    Oct 22, 2021
    Posts:
    7
    I am working on a 2d ship combat game, and to enable the enemy ai to fire cannonballs at the player I created two empty game objects (one for each side of the enemy) with colliders attached to define the enemy's firing range and detect when the player is in this range. These are on the "Enemy Target Detection Layer", and are parented to the enemy game object, which is on the "Default" layer. There are also wood pickups scattered around the map, which the enemy can pickup by colliding with. These pickups are on the "Default" layer. The player has two hitboxes, one on the "Default" layer, and on the "Enemy Target Detection" layer. In the Layer Collision Matrix in project settings, I have unchecked the box where the "Enemy Target Detection" layer, and the "Default" layer collide, however When the firing range hitbox collides with a wood pickup the enemy is credited with wood. I have also tried unchecking all the boxes on the collision matrix as a test, and the collisions still occured in the same way as before.

    This is the code that credits the enemy with wood:

    Code (CSharp):
    1. private void OnTriggerEnter2D(Collider2D collision)
    2.    {
    3.            if (collision.CompareTag("WoodPickup"))
    4.            {
    5.                wood++;
    6.            }
    7.    }
    Restarting the editor did not help, and I am lost on what else to check, so any help would be appreciated.

    Thanks,

    CT
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,859
    Child objects inherit the layer of their parent objects, so these colliders will be in fact on the 'Default' layer despite what might be visible in the inspector.

    You should be using ray-casts, overlap checks, or even just simple distance checks to determine when the player is in range.
     
    CTV123 likes this.
  3. CTV123

    CTV123

    Joined:
    Oct 22, 2021
    Posts:
    7
    Thanks for the info! I feel dumb now, but I did some more testing and found out that the layer collision matrix was being completely ignored by the game. Then I looked closer at the tabs on the left side of the screen and found out that it was because I was editing the matrix under the Physics tab instead of the Physics2D tab.
     
    spiney199 likes this.