# Physics.CheckSPhere is not working

I try to use Physics.CheckSphere to check if a coordonate is empty. I use three for loop to cycle threw a 2 x Y x 2 chunk. The problem is that it return true if the corner is touching the coordonate and its actually not even touching it, since I use a 0.5 sphere radius: if(Physics.CheckSphere(new Vector3(x,y,z), 0.5f) == false)...

(by the way the white cubes you can see on the picture is only to visualize where it detected empty space, they dont have any collider enabled)

and the chunks have a mesh collider, so why is it returning true when its no even touching it ?

if(Input.GetKeyDown("i")) {

for(int y = 0; y <= amp + 3; y++)
{
for(int x = 0; x <= chunkSize - 1; x++)
{
for(int z = 0; z <= chunkSize - 1; z++)
{
if(Physics.CheckSphere(new Vector3(Mathf.Round(x),Mathf.Round(y),Mathf.Round(z)),0.5f) == false)
{
Instantiate(grass,new Vector3(x,y,z), Quaternion.identity);
}
}
}
}
}

• Sounds like you should show us your loop code if you want help debugging it. You're probably checking a point or radius slightly off from where you think you are. – DMGregory Jun 26 '19 at 16:23
• there you go, I added my loop – Samuel Fyckes Jun 26 '19 at 16:59
• A 0.5 radius sphere placed at eg. (0, 0, 0) will have a diameter from (0, 0, -0.5) to (0, 0, 0.5). A 1-unit wide cube centered at (0, 0, 1) will cover the interval (0, 0, 0.5) to (0, 0, 1.5). So these two volumes will kiss (overlap at exactly one point) at (0, 0, 0.5). So, would we not expect an overlap to be detected in that case? – DMGregory Jun 26 '19 at 17:14
• A 1 sphere radius make it worse. – Samuel Fyckes Jun 26 '19 at 18:26
• Obviously, making the radius larger will lead to more collisions being detected, which is why I did not suggest such a thing. – DMGregory Jun 26 '19 at 18:27

Use a smaller detection radius. DMGregory points out that 0.49999997f would work.
            if(Physics.CheckSphere(new Vector3(Mathf.Round(x),Mathf.Round(y),Mathf.Round(z)),0.49999997f) == false)

• Epsilon is not an appropriate value to use here. It's the smallest representable float distinct from zero, or about 1.4 * 10^-45. But in the vicinity of 0.5f, we have precision to only about 1.5 * 10^-0.8. So 0.5f - Mathf.Epsilon rounds correctly to 0.5f and accomplishes nothing. The next representable float smaller than 0.5f is 0.49999997f – DMGregory Jul 7 '19 at 17:23