- Home /
Deploy object avoiding collisions
Hi,I have to instantiate a GameObject on a wall but I want to check that is not overlappingother GameObject already deployed on the wall. The positioning control starts from the exact center (both horizontal and vertical) of the wall and then the GameObject is moved to the right until the edge of the wall to see if it can be released. In chaste negative moves below and then the control starts from the left side of the wall to the right. Even here if the object is not in a valid position it continues to move down and then from left to right. Once he reach the bottom limit of the wall, the positioning control will be restarted from the top left of the wall and continues checking until it returns to the center. Someone can help me? Here is the code I've written:
IEnumerator checkPos (GameObject GO) {
Vector2 wallSize = Utilities.getWallSize();
Vector3 GOsize = Utilities.getObjectSize(GO);
float xLimit = wallSize.x - GOsize.x;
float yLimit = wallSize.y - GOsize.y;
Vector3 starPos = GO.transform.position;
bool isAllocable = false;
Bounds GoBound = GO.renderer.bounds;
GameObject[] objectsOnStage = GameObject.FindGameObjectsWithTag("Furniture");
foreach (GameObject model in objectsOnStage){
float ActualPosX = GO.transform.position.x;
float ActualPosY = GO.transform.position.y;
Bounds otherBound = model.renderer.bounds;
// object is colliding
if (GoBound.Intersects (otherBound) ){
Debug.Log (ActualPosX +" -- "+ xLimit);
if (ActualPosX <= xLimit){
Vector3 horizontalMove = new Vector3 (ActualPosX += 0.1f, ActualPosY, GO.transform.position.z);
GO.transform.position = horizontalMove;
}
Debug.Log (ActualPosX);
Debug.Log ("not deployable");
// object is deployable
} else {
Debug.Log ("deployable");
}
yield return null;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613122231im_/https://answers.unity.com/themes/thub/images/avi.jpg)