How to easily find nearest object?
Hello!! So... I' trying to make something like a simple bomberman.. So when I try to put the bomb I need to check if there is any bomb already here. I tried use Physics.OverlapSphere to get all colliders over bomb position.. but it doesn't work seccessfully... Sometimes it doesn't find bomb collider. Maybe I used this method not right. I found one more solve of this problem. I find all objects with tag "Bomb" and compare every object with needed position. It works, but I think that this is not perfect way. I need your help! please
Vector3 posOfBomb = new Vector3(Mathf.RoundToInt(transform.position.x), transform.position.y, Mathf.RoundToInt(transform.position.z));
GameObject[] foundArray = GameObject.FindGameObjectsWithTag(GV.BOMB_TAG);
if (foundArray.Length > 0)
{
foreach (var obj in foundArray.ToList())
{
if (obj.transform.position == posOfBomb)
{
return false;
}
}
return true;
}
else
{
return true;
}
I think that the Physics.OverlapSphere will helps my in my problem, but it doesn't. Maybe I did it not right. Can you tell about this method: all conditions for work with it?
How close is a bomb is already there? Is there a grid where a bomb can go or is free to go anywhere?
bomb can instantiates only in "roundToInt" position. So insatntiate method looks like "Vector3 posForBomb = new Vector3( $$anonymous$$athf.RoundToInt(transform.postition.x), transform.position.y, $$anonymous$$athf.RoundToInt(transform.position.z));