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?


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.

enter image description here

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.

  • I think you may have misunderstood. I'm only spherecasting against one object: a flat plane. It's hard to draw it in two dimensions, but my drawing is a sort of oblique projection with the horizontal line representing the flat surface of the plane and the diagonal line being a polygon edge in that plane. – IanLarson Sep 14 at 21:59
  • Indeed that might be the case. For debugging purposes if you would you could use Debug.DrawLine to visualize in-scene (like in my screenshots) the vectors you are getting wrong and right, that would help further clarify information. Also don't forget to enable Gizmo drawing in Unity's game view. – ivane Sep 15 at 13:25
  • I don't think what you're talking about in your answer is the problem. The cast isn't starting inside the collider and there's only one collider. SphereCastAll wouldn't be useful in giving any more information here, since there's only one collider: a flat plane facing upward. – IanLarson Sep 16 at 1:41

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.