Search Unity

Bug CastRay returning unexpected results given Collision Filter

Discussion in 'Physics for ECS' started by J_Rolph13, Jan 13, 2023.

  1. J_Rolph13

    J_Rolph13

    Joined:
    Jan 18, 2020
    Posts:
    3
    I'm attempting to create a turret selection system.
    A ray is cast from the player camera to mouse position.

    The ray is set up with the following filter;

    Code (CSharp):
    1. var rayInput = new RaycastInput()
    2.         {
    3.             Start = start,
    4.             End = end,
    5.             Filter = new CollisionFilter
    6.             {
    7.                 BelongsTo = 4u,
    8.                 CollidesWith = 1u
    9.             }
    10.         };
    These are the existing collision layers.
    upload_2023-1-13_21-19-55.png

    These are the collision filters of a turret. Collides with has 0, 3 and 4 ticked.
    upload_2023-1-13_21-21-30.png

    Collision filter of terrain pieces. Collides with has 0, 1, 2 and 3 ticked.
    upload_2023-1-13_21-22-8.png


    I'm expecting the raycast to only collide with Turrets, at which point I add a selected component. Otherwise, I remove all selected components.
    However, the selecting system is effectively working backwards, only adding components to terrain pieces and ignoring the turret.

    This code returns true.
    Code (CSharp):
    1. Debug.Log(CollisionFilter.IsCollisionEnabled(rayInput.Filter, new CollisionFilter { BelongsTo = 1, CollidesWith = 4 }));
    2.        
    This code returns false.
    Code (CSharp):
    1.  
    2.         Debug.Log(CollisionFilter.IsCollisionEnabled(rayInput.Filter, new CollisionFilter { BelongsTo = 1, CollidesWith = 4 }));
    Am I missing something obvious?
     
  2. Thygrrr

    Thygrrr

    Joined:
    Sep 23, 2013
    Posts:
    700
    Your filter matches "0: Terrain."

    These are bit masks and my mental crutch here is is "1 << 0", which equals 1.

    I think they should really change the labeling on these groups. The whole system is a bit janky and I never got the CategoryNames scriptable object thingy work.

    In other words, your "1: Turret" category is actually equal to 2u, so you are picking against the "0: Terrain" category, which is 1u.
     
    Occuros likes this.
  3. Pek1ng

    Pek1ng

    Joined:
    Dec 5, 2021
    Posts:
    4
    use PhysicsCategoryTags,look like that :
    Code (CSharp):
    1.             _collisionFilter = new CollisionFilter
    2.             {
    3.                 BelongsTo = new PhysicsCategoryTags
    4.                 {
    5.                     Category04 = true
    6.                 }.Value,
    7.  
    8.                 CollidesWith = new PhysicsCategoryTags
    9.                 {
    10.                     Category01 = true
    11.                 }.Value,
    12.             };
     
    Last edited: Jan 18, 2023
    Occuros likes this.