Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Collision Filters not working

Discussion in 'Physics for ECS' started by slims, Mar 4, 2021.

  1. slims

    slims

    Joined:
    Dec 31, 2013
    Posts:
    86
    I have some custom CollisionFilters setup like this:

    Code (CSharp):
    1.     public enum CollisionLayers : uint
    2.     {
    3.       Fleet = 1u << 0,
    4.       Asteroids = 1u << 1,
    5.       Enemy = 1u << 2,
    6.       Player = 1u << 3,
    7.     }
    8.  
    9.     public static CollisionFilter
    10.       AsteroidsFilter = new CollisionFilter
    11.       {
    12.         BelongsTo = (uint) (CollisionLayers.Asteroids),
    13.         CollidesWith = (uint) (CollisionLayers.Fleet | CollisionLayers.Enemy | CollisionLayers.Player | CollisionLayers.Asteroids),
    14.       },
    This is meant to mirror my physics categories.

    When I perform raycast with the Asteroid collision filter, the raycast still hits the player. Here's the raycast code (the filter being passed is the AsteroidFilter above):

    Code (CSharp):
    1.       hit = new RaycastHit();
    2.       var raycast = new RaycastInput
    3.       {
    4.         Start = start,
    5.         End = end,
    6.         Filter = filter,
    7.       };
    8.  
    9.       if (world.CastRay(raycast, out var closestHit))
    10.       {
    11.         hit = closestHit;
    12.         return true;
    13.       }
     
  2. milos85miki

    milos85miki

    Joined:
    Nov 29, 2019
    Posts:
    197
    AsteroidsFilter is meant to collide with Player, but it still depends on PlayerFilter that we don't see here. If PlayerFilter has Player in BelongsTo and Asteroids in CollidesWith, then the result makes sense.

    The easiest way to find out (and debug) whether 2 filters will enable collision is to call CollisionFilter.IsCollisionEnabled().
     
  3. slims

    slims

    Joined:
    Dec 31, 2013
    Posts:
    86
    I want the ray Filter to mean, "only collide with things that Belong To this filter". Are you saying this isn't what the semantics are in this case?
     
  4. milos85miki

    milos85miki

    Joined:
    Nov 29, 2019
    Posts:
    197
    No, the filter says "this object BelongsTo specified categories, which will collide only with CollidesWith categories". You can see the details in Unity.Physics.CollisionFilter.IsCollisionEnabled().
     
  5. tassarho

    tassarho

    Joined:
    Aug 25, 2019
    Posts:
    75
    had recently the same issue, in my case the raycast register the terrain and i just happen to let on the terrain belongsTo :everything
    My terrain physic shape (origin of my problem) by making belongs to everythings the raycast register it
    upload_2021-3-13_16-15-19.png
    at first i thought by assigning collideWith, will make the raycast collide with entity having ONLY the one register on the reycast's filter collide with, but in fact you have to disable this layer on all other entities you don't to collide with:
    upload_2021-3-13_16-18-41.png
    after removing the layer "UnitColision" from the terrain Entity.

    not sur if this is the same problem, hope it helps^^