Physics.RaycastAll misses a non-convex mesh colider

i’m moving a game object with a sphere colider (with is trigger) on update by changing its transform.psition. before the go is moved there is a physics.raycastall from the objects position to it’s next position. this works fine in all situations except when there is a mesh collider with the convex setting off. in that case the hit behaviour is erratic. sometimes it works, sometimes there is no hit before the go enters the mesh collider and sometimes there is a hit when the go exits the mesh colider.
increasing the sphere collider size doesn’t help. neither does performing the raycast on fixedupdate.

is this a known issue? and if yes, what is happening and are there workarounds?

I am visualizing my ray, and it is obviously hitting a non-convex mesh 4 times. RaycastAll returns 1 hit. The documentation doesn’t mention this limitation, but apparently its not meant for non-convex meshes. Which is unfortunate.

i think to remember that in my case the problem was a wrong face orientation. some face normals were pointing backwards into the mesh and therefor “invisible” for the ray coming from outside.

This was only a few minutes of code (excuse the Utils, they do what you would expect.) It was much longer to discover the undocumented behavior.

//Raycast All that works with non-convex mesh colliders

public static RaycastHit[ ] RaycastAllNonConvex( Vector3 origin, Vector3 destination, int mask, bool show ) {

List hits = new List();
bool clear = false;
Vector3 delta = destination - origin;
Vector3 dir = delta.normalized;

while ( !clear )
{
RaycastHit hit;
float dist = delta.magnitude;
if ( Util.Raycast( origin, destination, out hit, mask, show ) )
{
if ( show ) Util.DrawCross( hit.point, Color.magenta );
origin = hit.point + dir * 0.01f; //rem that collision is inclusive
hits.Add( hit );
}
else
{
clear = true;
}
}
return hits.ToArray();
}

1 Like