- Home /
OverlapSphere picking colliders that are not inside
I have one issue that I dont understand?!
I made a simple test: I have in my scene box and camera. The box has a collider component and it also has script that implements interface ISmashable. My idea was to move the camera and to hit the box, and when I hit it, directly it sends the camera to a start position of:
new Vector3(transform.position.x, 15, transform.position.z);
for that purpose I created this script which is directly called in the update method of the camera:
void Hit()
{
var coll = Physics.OverlapSphere(transform.position, 0.5f);
foreach (var item in coll)
{
var smash = item.GetComponents(typeof(ISmashable));
if (smash.Length == 0)
{
return;
}
for (int i = 0; i < smash.Length; i++) {
Debug.Log("hit");
transform.position =
new Vector3(transform.position.x,15,transform.position.z);
}
}
}
The problem is that when I hit the box it does not stopping writing "hit" in the console even when I am actualy far away from the box, and basically Physics.OverlapSphere should return me empty array.
Please can anyone explain me where I'm do wrong ?!
I see nothing wrong with the code. I suggest you put a sphere around this game object without a collider with a size of 1.0 (radius of 0.5) to visualize the overlap. In addition, output the name of the game object for each collider
Thanks for the comment, well I put the sphere around the camera without any collider, yes. And the box has collider, but it still doesnt fix the problem, it keep running and doesn't stop.