How to set up LayerFilters with script?

I am trying to get a Raycast to only target layer 5.

public enum ECSLayers : uint
{
    UIselect = 1u << 5,

}
unmanagedData.ValueRW.SelectFilter = new CollisionFilter
  {
      BelongsTo = (uint)ECSLayers.UIselect,
      CollidesWith = (uint)ECSLayers.UIselect,
      GroupIndex = 0,
  };
  RaycastInput input = new RaycastInput
  {

      Start = ray.origin,
      End = ray.GetPoint(10000f) - ray.origin,
      Filter = unmanagedData.SelectFilter,
  };
  //   Debug.DrawLine(input.Start, input.End, Color.green, 5f);

  RaycastHit hit = new RaycastHit();
  physicsWorld.CastRay(input, out hit);

9712129--1387795--upload_2024-3-20_18-47-17.png

What’s your question? “How do you do this” when you show part of the stuff you use without a full picture of what’s happening or what’s wrong is a little strange. How are you setting up each of the objects you’re working with? What resources did you use to get to this point? How did you determine something’s wrong? Help us help you.

Note that RaycastInput takes a start and end position. Subtracting ray.origin is wrong.

I am trying to select a entity that has layer 5 (UI) and exclude all other layers by using the CollisionFilter

The Raycast direction is based on the mouse and it has the exact direction of where I’m hovering my mouse over. (I see now that mentioning the raycast in my original post was misleading)

The problem I’m having is setting UIselect to only target layer 5 (UI) and ignoring all other layers.
I could not find many posts about layers on the forum, the first one I found was about 4 years old and was UIselect = 5 << 0, this worked for a time but I eventually found out that it was including other layers. Another post suggested UIselect = 1u << 5 but this doesn’t seem to select any layers.

The reason for my post is to find out the correct way of setting layers in 2024.

Subtracting ray.origin is still wrong. RaycastInput takes a start position and an end position, not a start position and a direction. You should just pass ray.GetPoint(…) without subtracting the origin.

Using bitmasks for layer selection is pretty simple and well-understood, you just take 1 and left-shift it by the layer index you want. To target layer index 5, 1 << 5 is correct. If things work with 5 << 0 (I have no idea why anyone would write that, it’s just equal to 5), which matches layer index 0 and 2, and / or using the correct mask value 1 << 5 doesn’t work, you probably didn’t correctly set up the layers for the objects that you’re trying to check. You should verify that the objects in question are indeed set up to be on layer 5, and also make sure the global physics collision matrix is set up so layer 5 can collide with itself.

1 Like

After testing, subtracting by ray origin does nothing so I’ve taken it out.

1 << layer works, thank you. It also seems to work with multiple layers 1 << 5 | 1 << 18 | 1 << 17 | 1 << 0, so that’s good.