As the title says - the layermasks are not working correctly.
This is my layermask:
private static int _PurrtatoPlacementAnchorLayerMask = -1;
public static int PHYSLAYER_PurrtatoPlacementAnchorLayerMask
{
get
{
if (_PurrtatoPlacementAnchorLayerMask == -1)
_PurrtatoPlacementAnchorLayerMask = LayerMask.GetMask(new string[]{"PurrtatoDragging",
"PurrtatoDraggingBadConnect",
"PurrtatoDraggingCanConnect",
"Purrtato"});
return _PurrtatoPlacementAnchorLayerMask;
}
}
I then perform my raycasts in code:
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:
if (MiddleRaycast.collider != null)
{
distanceList.Add(new distanceElements()
{
distance = Vector3.Distance(middlePoint, MiddleRaycast.point),
linkPoint = MiddleRaycast.point,
});
Debug.LogError("MID COLLIDER: " + MiddleRaycast.collider.name + " / " + LayerMask.LayerToName(MiddleRaycast.collider.gameObject.layer), MiddleRaycast.collider.gameObject);
Debug.DrawLine(middlePoint, MiddleRaycast.point, Color.green, 100f);
}
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:
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!