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

by IanLarson   Last Updated September 14, 2018 01:13 AM

enter image description here

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?



Answers 1


Interesting to see what does Physics.SphereCastAll returns and see weather intersection (green vector) with horizontal line (horizontal black line) is detected at all.

Results shall be like as shown bellow enter image description here

But when sphere is touching/overlapping surfaces result will be like enter image description here

This is because:

For colliders that overlap the sphere at the start of the sweep, RaycastHit.normal is set opposite to the direction of the sweep, RaycastHit.distance is set to zero, and the zero vector gets returned in RaycastHit.point. You might want to check whether this is the case in your particular query and perform additional queries to refine the result. Passing a zero radius results in undefined output and doesn't always behave the same as Physics.Raycast.

ivane
ivane
September 14, 2018 08:25 AM

Related Questions



Why are my smooth curved meshes bumpy?

Updated July 19, 2015 13:05 PM

Finding the closest point on a concave mesh (Unity C#)

Updated February 24, 2018 00:13 AM