TrajectoryObject.Cast(...) Doesn't Sort Hits by Distance

airoll

Member
Hi, I noticed in the transition from UCC2 to UCC3, TrajectoryObject went from using SingleCast(...) which did casts that returned the closest hit, to Cast(...) which can return multiple hits. However, these hits are not ordered by distance (https://forum.unity.com/threads/raycastnonalloc-sort-collision-from-furthest-to-closest.588013/). This results in a scenario where if I have a shield, my projectile can "hit" the shield as well as my character, and because only the first collider is used to assign m_RaycastHit, the projectile "hits" the character instead of the shield.

This can be fixed by adding a line:

C#:
Array.Sort(m_CombinedRaycastHits, 0, hitCount, m_RaycastHitComparer);

after we perform the Physics.XXXCastNonAlloc. Additionally, if we do this, then we can replace the assignment for closestRaycastHit to the following:

C#:
var closestRaycastHit = m_CombinedRaycastHits[i];
 
It looks like it's getting the closest raycast if m_OwnerCollisionCheck is enabled, but not otherwise. Thanks, I'll take a look. You can use the QuickSelect class instead of sorting the entire array.
 
Top