Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved How to set LayerMask for CollisionWorld.CastRay?

Discussion in 'Entity Component System' started by vectorized-runner, Dec 20, 2020.

  1. vectorized-runner

    vectorized-runner

    Joined:
    Jan 22, 2018
    Posts:
    383
    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:

    Code (CSharp):
    1.         public static RaycastInput GetRayInput(float3 start, float3 end, LayerMask collisionMask)
    2.         {
    3.             return new RaycastInput
    4.             {
    5.                 Start = start,
    6.                 End = end,
    7.                 Filter = new CollisionFilter
    8.                 {
    9.                     BelongsTo = ~0u,
    10.                     CollidesWith = (uint)collisionMask.value,
    11.                     GroupIndex = 0
    12.                 }
    13.             };
    14.         }
    15.  
    16.         public static bool IsVisibleThroughGeometry(CollisionWorld collisionWorld, float3 pointA, float3 pointB, LayerMask obstacleMask)
    17.         {
    18.             var input = GetRayInput(pointA, pointB, obstacleMask);
    19.             collisionWorld.CastRay(input, out var closestHit);
    20.  
    21.             LogEntityName(closestHit.Entity);
    22.             // If does not hit an obstacle, then it is visible
    23.             return closestHit.Entity == Entity.Null;
    24.         }
    25.  
    26.         [BurstDiscard]
    27.         static void LogEntityName(Entity entity)
    28.         {
    29.             Debug.Log(World.DefaultGameObjectInjectionWorld.EntityManager.GetName(entity));
    30.         }
    Logging the Entity name shows that hit entity does not belong to the layer I have provided as LayerMask. What am I missing here?
     
  2. UsmanMemon

    UsmanMemon

    Joined:
    Jan 24, 2020
    Posts:
    87
    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?
     
    vectorized-runner likes this.
  3. vectorized-runner

    vectorized-runner

    Joined:
    Jan 22, 2018
    Posts:
    383
    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.