Why is my spherecast returning an incorrect hit on mesh collider?

124159-spherecastproblem.png

I have a mesh collider that is largely a flat plane facing up. Any SphereCast against that surface should return a normal of Vector3(0, 1, 0), but that isn’t always happening. When near an edge on the mesh, the cast will penetrate the face and register the hit against the edge as if it was a corner when it’s really just a polygon edge on a flat surface This is giving me inaccurate hit points and normals.

Hopefully the image above is interpretable. The light blue circle is where the SphereCast should stop and the pink circle is where it seems to be stopping. Instead of hitting the face (horizontal black line) and returning the proper normal (green line), it’s ignoring the face and hitting the tri’s edge (diagonal black line) and returning a lerped normal (red line) as if it were a corner.

Is the SphereCast that inaccurate against mesh colliders, or am I doing something wrong? Is there anything I can do to fix this?


Edit: I’ve added a screenshot to illustrate exactly what’s happening. The black line/sphere are the cast, the green line is what the normal should be, the red line is the normal the SphereCast is returning. The normals differ by as much as 0.05m. The mesh is the default plane mesh. It seems like when the cast hits near a polygon edge, it snaps to the edge, giving an incorrect hit point and normal.

126375-screen-shot-2018-10-19-at-13425-pm.png

@ianlarson and anyone else experiencing this. I had pretty bad accuracy issues with sphere-casts just now with non-convex mesh colliders, and it turns out it helps a lot if the start of the ray is closer to the surface.


What I’m doing now is start with my usual sphere-cast to get the initial hit pos, and then do another sphere-cast starting from a point pulled back just a bit from the initial hit distance. It’s still not perfect, but it gives something like 1/10th of the visible error I had before in both the hit point and normal.