Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question How to set up LayerFilters with script?

Discussion in 'Physics for ECS' started by SapphireGames2025, Mar 20, 2024.

  1. SapphireGames2025

    SapphireGames2025

    Joined:
    Mar 14, 2019
    Posts:
    31
    I am trying to get a Raycast to only target layer 5.
    Code (CSharp):
    1. public enum ECSLayers : uint
    2. {
    3.     UIselect = 1u << 5,
    4.  
    5. }
    Code (CSharp):
    1. unmanagedData.ValueRW.SelectFilter = new CollisionFilter
    2.   {
    3.       BelongsTo = (uint)ECSLayers.UIselect,
    4.       CollidesWith = (uint)ECSLayers.UIselect,
    5.       GroupIndex = 0,
    6.   };
    7.  
    Code (CSharp):
    1.   RaycastInput input = new RaycastInput
    2.   {
    3.  
    4.       Start = ray.origin,
    5.       End = ray.GetPoint(10000f) - ray.origin,
    6.       Filter = unmanagedData.SelectFilter,
    7.   };
    8.   //   Debug.DrawLine(input.Start, input.End, Color.green, 5f);
    9.  
    10.   RaycastHit hit = new RaycastHit();
    11.   physicsWorld.CastRay(input, out hit);
    upload_2024-3-20_18-47-17.png
     
  2. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    830
    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.
     
  3. SapphireGames2025

    SapphireGames2025

    Joined:
    Mar 14, 2019
    Posts:
    31
    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.
     
  4. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    830
    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.
     
    SapphireGames2025 likes this.
  5. SapphireGames2025

    SapphireGames2025

    Joined:
    Mar 14, 2019
    Posts:
    31
    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.