Search Unity

CastCollider

Discussion in 'Entity Component System' started by digitalThorns, Jul 9, 2019.

  1. digitalThorns

    digitalThorns

    Joined:
    Apr 7, 2014
    Posts:
    8
    I am wanting to filter triggers from a collider cast. What is the easiest way to check if a hit has a certain component on it or if the hit is a trigger? I'm working from the ECS Physics Samples. Here is my current code.

    Code (CSharp):
    1.    MaxHitsCollector<ColliderCastHit> castCollector = new MaxHitsCollector<ColliderCastHit>(1.0f, ref castHits);
    2.       ColliderCastInput castInput = new ColliderCastInput()
    3.       {
    4.         Collider = collider,
    5.         Orientation = quaternion.identity,
    6.         Start = origin,
    7.         End = origin + rayDirection
    8.       };
    9.       world.CastCollider(castInput, ref castCollector);
    10.    
    11.       for(int hitIndex = 0; hitIndex < castCollector.NumHits; hitIndex++)
    12.       {
    13.         var hit = castCollector.AllHits[hitIndex];
    14.         // Wanting to check if hit is a trigger here.
    15.       }
    16.     }
     
  2. florianhanke

    florianhanke

    Joined:
    Jun 8, 2018
    Posts:
    426
    Not sure about the trigger, but for component checking you can use

    Code (CSharp):
    1. var entity = collisionWorld.Bodies[hit.RigidBodyIndex].Entity
    to extract the entity, which you can then use to check for a certain component (via HasComponent).
    There may be a more straightforward way, but this is the one I use currently.