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

Bug Unity Physics raycast not interacting with collision filter

Discussion in 'Entity Component System' started by Unique-Player, Dec 13, 2022.

  1. Unique-Player

    Unique-Player

    Joined:
    May 6, 2017
    Posts:
    59
    I am launching raycast with collision filter, but it is not hitting the desired object at all.

    This is the function for raycasting
    Code (CSharp):
    1.     private bool Raycast(float3 fromPosition, float3 toPosition, out Unity.Physics.RaycastHit raycastHit, uint collidesWith = ~0u)
    2.     {
    3.         PhysicsWorldSingleton physicsWorld = SystemAPI.GetSingleton<PhysicsWorldSingleton>();
    4.  
    5.         RaycastInput raycastInput = new RaycastInput
    6.         {
    7.             Start = fromPosition,
    8.             End = toPosition,
    9.             Filter = new CollisionFilter
    10.             {
    11.                 BelongsTo = ~0u,
    12.                 CollidesWith = collidesWith,
    13.                 GroupIndex = 0
    14.             }
    15.         };
    16.  
    17.         raycastHit = new Unity.Physics.RaycastHit();
    18.         if (physicsWorld.CastRay(raycastInput, out raycastHit))
    19.         {
    20.             return true;
    21.         }
    22.         else
    23.         {
    24.             return false;
    25.         }
    26.     }
    This is the code i am calling this function from. My desired object has Collision Filter "BelongsTo" set to "2: Object"
    Code (CSharp):
    1. Raycast(lastPointPosition + new float3(0, 0.1f, 0), direction * distance, out segmentRaycastHit, 1u << 2)
    With drawray i am checking if objects should be hit, and the raycast line is intersecting the object, but raycast is not hitting it.
    Without CollisionFilter raycast hits the object.
     
  2. vectorized-runner

    vectorized-runner

    Joined:
    Jan 22, 2018
    Posts:
    383
    It should work, maybe check your object and ensure it has the layer assigned (not overridden with material template or something)
     
  3. Unique-Player

    Unique-Player

    Joined:
    May 6, 2017
    Posts:
    59
    It has layer assigned to, collision filter itself produces strange results. With it set hit to everything i am able to get hit on desired object, but another issue arises that ray starts to hit invisible, not present in scene, object. I should note that, i am using instantiated entities and seems there might be some issues related with that, im trying to figure this part out.
     
  4. piginaust

    piginaust

    Joined:
    May 23, 2022
    Posts:
    14
    You may want to check your collision filter at physic shape. Both physic shape and your code should match as far as I know. My raycast works btw.
     
  5. Unique-Player

    Unique-Player

    Joined:
    May 6, 2017
    Posts:
    59
    I tried checking what kind of result it gives me by getting PhysicsShape component and its filter BelongsTo parameter. I got uint 4. Then i tried to directly put uint 4 into the collision filter and it still ignored any collision.
     
  6. joshshin

    joshshin

    Joined:
    Sep 15, 2023
    Posts:
    1
    Currently experiencing the same issue