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.

Unity ECS Physics how to use CollisionFilter , RaycastHit [RESLVED]

Discussion in 'Physics for ECS' started by Opeth001, Jul 17, 2019.

  1. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    986
    hi everyone.
    1) is there a way to set a detailed CollisionFilter layers for a RaycastInput ?
    eg : belongsto Projectile , Collides with Players,Environment, Destructables ...
    Code (CSharp):
    1.  
    2.                 // RayCast Input
    3.                 RaycastInput input = new RaycastInput()
    4.                 {
    5.                      Start = projectileData.lastFramePosition,
    6.                      End = translation.Value,
    7.                      Filter = CollisionFilter.Default // Standard Filter
    8.                 };
    9.  

    2) how can i get The Collider Layer from a RaycastHit ?
     
    BigRookGames likes this.
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,452
    What's stopping you setting the filter layer?

    Code (CSharp):
    1. Filter = new CollisionFilter
    2. {
    3.     BelongsTo = 0xffffffff,
    4.     CollidesWith = this.settings.ObserverLayer,
    5.     GroupIndex = 0,
    6. },
    7.  
    You can get the layer of the objects you hit from the Collider

    Code (CSharp):
    1.             for (var i = 0; i < hits.Length; i++)
    2.             {
    3.                 var body = bodies[hits[i]];
    4.                 var filter = body.Collider->Filter
    (this requires unsafe context)
     
    Opeth001 likes this.
  3. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    986
    how can i set muliple layers for the CollidesWith field?
    eg i want to specify that my projectile can collide with multiple layers.

    where di you find these api ?
    the official documentation is outdated.
     
  4. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,452
    I don't really read documentation so I can't say if it exists there. I just read the source code.

    As for your question about layers, CollidesWith is a mask field. 0xffffffff is just the hex code for everything.

    If you only want layer 1 or 2 (and not layer 0) for example you could just use

    Code (CSharp):
    1. CollidesWith = 1u << 1 | 1u << 2,
    It's very similar to regular LayerMasks

    https://docs.unity3d.com/ScriptReference/Physics.Raycast.html

    Except it's a uint

    Code (CSharp):
    1.         // Bit shift the index of the layer (8) to get a bit mask
    2.         int layerMask = 1 << 8;
    3.  
    4.         // This would cast rays only against colliders in layer 8.
    5.         // But instead we want to collide against everything except layer 8. The ~ operator does this, it inverts a bitmask.
    6.         layerMask = ~layerMask;
     
  5. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    986
  6. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,351
    Or just use something like this https://jacksondunstan.com/articles/5126 that makes setting complex masks easy.

    I map Unity layers to Unity.Physics layers a lot.

    Code (csharp):
    1.  
    2. public static CollisionFilter LayerMaskToFilter(LayerMask mask)
    3.         {
    4.             CollisionFilter filter = new CollisionFilter()
    5.             {
    6.                 BelongsTo = (uint)mask.value,
    7.                 CollidesWith = (uint)mask.value
    8.             };
    9.             return filter;
    10.         }
    11.  
    12.         public static CollisionFilter LayerToFilter(int layer)
    13.         {
    14.             if (layer == -1)
    15.             {
    16.                 return CollisionFilter.Zero;
    17.             }
    18.  
    19.             BitArray32 mask = new BitArray32();
    20.             mask[layer] = true;
    21.  
    22.             CollisionFilter filter = new CollisionFilter()
    23.             {
    24.                 BelongsTo = mask.Bits,
    25.                 CollidesWith = mask.Bits
    26.             };
    27.             return filter;
    28.         }
    29.  
     
  7. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    986
    thank you!
    it's easy of use ^^