- Home /
Why do I get a triangleIndex of -1 when I SphereCast a convex MeshCollider?
If I use Physics.SphereCastAll and it hits a MeshCollider that has it's Convex property set, the resulting RayCastHit has a triangleIndex of -1.
If however, I use Physics.RayCast, then I get a valid triangle index for the same MeshCollider. Is this a bug, or is there a good reason for this?
The documentation says 'Triangle index is only valid if the collider that was hit is a MeshCollider.' - it doesn't mention convex or concave colliders.
Answer by Owen-Reynolds · Oct 31, 2013 at 05:56 PM
I'm just guessing, but it may happen whenever you hit one of the "fake" faces created to make the object convex.
The triangle index might be for the original mesh, so invalid for the new faces. Then the ray vs. sphere would be just a coincidence -- the sphere just happened to hit an "added" face.
It looks like you could be right!
It does seem consistent behaviour though - I guess the sphere cast algorithm always uses the convex representation, and the ray cast just used the original mesh (which would make sense).
I've had to re-raycast each hit detected by the spherecast, which seems to work, although it's quite inefficient.