Search Unity

LayerMasks Do Not Function on Physics2D.Raycast in Unity 2019.3.7f1

Discussion in 'Scripting' started by RedVonix, May 12, 2020.

  1. RedVonix

    RedVonix

    Joined:
    Dec 13, 2011
    Posts:
    422
    As the title says - the layermasks are not working correctly.

    This is my layermask:

    Code (CSharp):
    1.     private static int _PurrtatoPlacementAnchorLayerMask = -1;
    2.     public static int PHYSLAYER_PurrtatoPlacementAnchorLayerMask
    3.     {
    4.         get
    5.         {
    6.             if (_PurrtatoPlacementAnchorLayerMask == -1)
    7.                 _PurrtatoPlacementAnchorLayerMask = LayerMask.GetMask(new string[]{"PurrtatoDragging",
    8.                                                                                    "PurrtatoDraggingBadConnect",
    9.                                                                                    "PurrtatoDraggingCanConnect",
    10.                                                                                    "Purrtato"});
    11.  
    12.             return _PurrtatoPlacementAnchorLayerMask;
    13.         }
    14.     }
    I then perform my raycasts in code:
    Code (CSharp):
    1.  
    2. RaycastHit2D MiddleRaycast = Physics2D.Raycast(middlePoint, (middlePoint - linkPosition).normalized, maxRaycastDistance, ~PurrtatoValues.PHYSLAYER_PurrtatoPlacementAnchorLayerMask);                      
    We want to hit everything NOT in the LayerMask, therefore I have flipped the bits on the layermask. Based on this, the "Purrtato" layermask should not be getting hit.

    I then have code that checks if the MiddleRaycast.collider is null or not, and handles this as necessary, like this:
    Code (CSharp):
    1.                     if (MiddleRaycast.collider != null)
    2.                     {
    3.                         distanceList.Add(new distanceElements()
    4.                         {
    5.                             distance = Vector3.Distance(middlePoint, MiddleRaycast.point),
    6.                             linkPoint = MiddleRaycast.point,
    7.                         });
    8.                         Debug.LogError("MID COLLIDER: " + MiddleRaycast.collider.name + " / " + LayerMask.LayerToName(MiddleRaycast.collider.gameObject.layer), MiddleRaycast.collider.gameObject);
    9.                         Debug.DrawLine(middlePoint, MiddleRaycast.point, Color.green, 100f);
    10.                     }
    Based on all of the above, the "Purrtato" LayerMask, again, should never get hit. However, in testing this code, it gets hit, as is evident from this debug output:
    Code (CSharp):
    1. MID COLLIDER: Tater_Guard(Clone) / Purrtato
    Note that I've found numerous issues related to LayerMask in Physics2D.Raycast in this version of Unity, but all issues are similar to this, with layers getting hit that are clearly masked out. This issue just shows the issue pretty strongly and specifically.

    Can anyone spot something wrong in my code, of verify that LayerMasks are indeed broken for 2D Physics in Unity 2019.3.7f1?

    Thank you!
     
    Last edited: May 12, 2020
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    If you can prove this with hard numbers, ie., I ask to mask out layer 13 and you still hit it, then make a tiny project and report the bug to Unity. They really do fix bugs like this.

    If you cannot repro this super-easy obvious bug, then go over your code until you find out why your above code, which is far too complicated to reason about by study, differs from the proven trivial case.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    I'd be surprised if there were a bug here because there's been no changes to this area of 2D physics for a long time and all the raycasts lead to the same underlying function but also I've seen no bug reports or mention of such issues on the forums, Reddit etc. I took a quick look at the physics code and nothing seems wrong.

    The only thing that stands out in your code is using - (minus) to invert the bits in the mask which isn't correct; you should use ~ (tilde) instead to give you the bitwise complement. Not sure though if that's the source of your issues.

    EDIT: I looked again and you are using ~ (looked like a minus when I looked at it on the phone).

    If it's not that then would you mind sharing a small reproduction project either as a bug report, host it yourself or DM me your email and I'll create a workspace where you can upload it?

    Note: I didn't see this post because I tend not to monitor this forum as it's way too noisy. I would recommend posting in the physics forum for stuff like this where I "hang out". ;)
     
    Last edited: May 19, 2020
    Kurt-Dekker likes this.