Search Unity

Bug PhysicsCollider.Value.Value.CastRay()

Discussion in 'Physics for ECS' started by BigBite, Jun 5, 2020.

  1. BigBite

    BigBite

    Joined:
    Feb 20, 2013
    Posts:
    108
    Hello, don't know if this is a bug, but using PhysicsCollider.Value.Value.CastRay() reports a hit even when the ray is totally contained inside the collider. Is that intended?
     
  2. thelebaron

    thelebaron

    Joined:
    Jun 2, 2013
    Posts:
    857
    Its kinda by design so until there's another way to ignore itself(which afaik is planned) you need to use a custom collector. there's an example in the official repo
    https://github.com/Unity-Technologi...ommon/Scripts/MousePick/MousePickBehaviour.cs


    To cut to the chase though here's what I've been using for my own shooting raycasts(note theres code that isnt used, just havent gotten around to tidying this up)

    Code (CSharp):
    1.  
    2.     public struct IgnoreSpecificEntityCollector: ICollector<RaycastHit>
    3.     {
    4.         public Entity IgnoreEntity;
    5.  
    6.         public bool EarlyOutOnFirstHit => false;
    7.         public float MaxFraction { get; private set; }
    8.         public int NumHits { get; private set; }
    9.  
    10.         private RaycastHit m_ClosestHit;
    11.         public RaycastHit Hit => m_ClosestHit;
    12.  
    13.         public IgnoreSpecificEntityCollector(Entity ignoreEntity,float maxFraction)
    14.         {
    15.             IgnoreEntity = ignoreEntity;
    16.             //m_World = world;
    17.             m_ClosestHit = default(RaycastHit);
    18.             MaxFraction = maxFraction;
    19.             NumHits = 0;
    20.         }
    21.  
    22.         #region ICollector
    23.  
    24.         public bool AddHit(RaycastHit hit)
    25.         {
    26.             if (hit.Entity.Equals(IgnoreEntity))
    27.                 return false;
    28.        
    29.             MaxFraction = hit.Fraction;
    30.             m_ClosestHit = hit;
    31.             NumHits = 1;
    32.      
    33.             return true;
    34.         }
    35.  
    36.         #endregion
    37.     }
    to use it

    Code (csharp):
    1.  
    2. var collector = new IgnoreSpecificEntityCollector(entity, 1.0f);
    3. var hit = CollisionWorld.CastRay(rayInput, ref collector);
    4.