SphereCast is buggy when casting against a mesh collider generated at runtime

Hi,
I’m working on a game that involves dynamic mesh modification, and I was using SphereCasting when I noticed a bizarre bug: mesh colliders created at runtime get weird innaccuracies.

I created a fresh project in 5.5.1f1 to test this out. Here is a gif below showing the issue.

On the left is a standard quad I created in the scene before running. Casting against it results in no strange behaviour. On the right is a quad I created at runtime (just using Create->Quad). You’ll notice the spherecast flickers in and out around the edges (this indicates the cast is returning false). For some reason, the cast is not properly detecting the edges of the dynamically created quad.

I’d usually just make a bug report and go with it, but this feels like it might have some sort of solution (am I supposed to request Unity rebuild it’s internal octree or something?) so I figured a thread might be warranted. If nobody has any ideas I’ll submit it as a bug report. Either way, I’ve attached the sample project below.

2983885–221982–SphereCastTest.zip (3.41 MB)

TLDR; try avoiding using Mathf.Infinity in your spherecast distance parameter.

I dont have unity 5.5, so I couldnt test your scene, but testing in unity 5.4.0f3 I was able to see something strange. It seems it has nothing to do with creating colliders at runtime.

I created a Plane in the editor then pressed play. (The plane scale is 1,1,1)
I moved the spherecast around the plane to see that all works as expected, and it seems to.
I then disabled the Plane and re enabled it.
I moved the spherecast around the plane, and now it seems in some spots the spherecast seems to just miss the plane, while a regular raycast detects it.
Whats even more weird is, now if I keep the spherecast at the spot that it wasnt able to detect and then move the plane around, it seems the spherecast now does not detect a much much bigger area (almost like there is a wall that it wont detect past, so maybe its a sign the issue has something to do with the bounding box detection?), but if I move the spherecast a tiny bit, it suddenly detects things.
Not only that, but if I leave the spherecast on a spot it can detect, and then move the plane around, it seems everything is fine again.
Its almost like the issue has something to do with moving the sphere which controls the spherecast.
The issue doesnt seem to be related to mesh colliders either, since the same thing happens with a box collider, even with its Y value raised. Although sometimes changing it to a box collider does fix it? Maybe its just updating the collider? I noticed one too that I set the collider to convex then back to non convex and it fixed itself.
Setting the mesh collider to convex seems to remove the issue, BUT, the convex mesh collider of a plane seems to be much bigger than the actual plane (seems to be 2x bigger)? This might help find where the bug is. If I take the box collider and extend it in the X and Z, then it too will fix the issue, but now the collider is much bigger than the actual plane. Even during edit mode, you can see that a convex plane collider is bigger than the plane.
This seems to be a bug that was reported and supposedly fixed in 5.5, so maybe this isnt whats causing us issues.

I also noticed I left the collider on the sphere I had the spherecast script on, but removing it didnt fix the bug.
I created a cube at runtime, scaled it to 5,5,5 to be easier to hit, and saw the same issue.
It seems to be easier to get the bug on newly created objects at runtime, but Im pretty sure I have seen the bug happen on a plane created in edit time then disabled/enabled, multiple times, but maybe I did something else to cause it?

However, after playing around with it, I think the issue is using Mathf.Infinity as the distance for the spherecast. Lowering it to something like 1000 or whatever seems to have gotten rid of the issue, then when setting back to Mathf.Infinity it showed up again.
So maybe it has something to do with floating point errors?

Edit - If Mathf.Infinity was the issue on your side, id count that as a bug worth telling unity about since the default behaviour of any cast is to use Mathf.Infinity, which can cause a load of bugs. Ill leave it to you to file the bug report ^_~

1 Like

Mathf.Infinity was the cause! Thanks so much!

…that is pretty damn bizarre overall. I’ll have to sit down and read through the PhysX source to see what the deal is. Gonna file a bug report with my attached project, since it’s clearly the issue.

2 Likes