Question on preventing blocks to be moved to an occupied position

I created special blocks that move around randomly when hit, until all breakable blocks are destroyed – then they are switched to breakable blocks as well.

Now, everything works fine but the moving blocks overlap one another, as their position is set randomly within the scene. I tried making sure that doesn’t happen with Physics.CheckSphere, but I can’t get it to work, it always returns “nothing here”, so it somehow doesn’t seem to “see” the other GameObjects. Can anyone help?

private void MoveBlock()
 
 {
 
 float randX = Random.Range(xMinBoundary, xMaxBoundary);
 
 float randY = Random.Range(yMinBoundary, yMaxBoundary);
 
 Vector2 target = new Vector2(randX, randY);
 
 
 float radius = 10f;
 
 
 
 if (Physics.CheckSphere(target, radius))
 
 {
 
 Debug.Log("found something");
 
 transform.localPosition = target * 20;
 
 }
 
 else
 
 {
 
 Debug.Log("found nothing");
 
 transform.localPosition = target;
 
 }
 
 
 }

Physics.checksphere wants a vector3. What if you tried transform.position instead when you call it.

You could also pause the game when you overlap and check the actual transforms to confirm overlap.

Also you are calling the MoveBlock() in the update method I assume? I was thinking you may have been calling it on start. But your code doesn’t show.

Privacy & Terms