How to set LayerMask for CollisionWorld.CastRay?

I just want to use it like how you pass LayerMask to Physics.Raycast, but I can’t seem to get it working.
Here is my ray code:

        public static RaycastInput GetRayInput(float3 start, float3 end, LayerMask collisionMask)
        {
            return new RaycastInput
            {
                Start = start,
                End = end,
                Filter = new CollisionFilter
                {
                    BelongsTo = ~0u,
                    CollidesWith = (uint)collisionMask.value,
                    GroupIndex = 0
                }
            };
        }

        public static bool IsVisibleThroughGeometry(CollisionWorld collisionWorld, float3 pointA, float3 pointB, LayerMask obstacleMask)
        {
            var input = GetRayInput(pointA, pointB, obstacleMask);
            collisionWorld.CastRay(input, out var closestHit);

            LogEntityName(closestHit.Entity);
            // If does not hit an obstacle, then it is visible
            return closestHit.Entity == Entity.Null;
        }

        [BurstDiscard]
        static void LogEntityName(Entity entity)
        {
            Debug.Log(World.DefaultGameObjectInjectionWorld.EntityManager.GetName(entity));
        }

Logging the Entity name shows that hit entity does not belong to the layer I have provided as LayerMask. What am I missing here?

LayerMask shouldn’t be a problem here. There seems to be another problem, are your physicsCategoryNames and layermask identical? and is your obstacle’s belongsTo correctly assigned?

1 Like

Oh wow. I didn’t realize I needed to define physicsCategoryNames, for some reason I thought setting the gameobject layer would automatically update physics body after conversion. So using LayerMask doesn’t make sense then.