- Home /
How can i avoid any gameobject overlapping when instantiate the gameobjects ?
This is how i instantiate the gameobjects in a specific area.
The problem is if the scale of the objects for example is 30,30,30 when it's 5,5,5 it's working fine but 30,30,30 make most of the objects to touch or be one on the other since there is no enough space for the objects.
So i wonder what should be the logic here ? To remove number of objects until there will be enough space then change the left objects positions so it won't overlapping ? Or maybe keep the number of objects but reduce the scaling until they will not overlapping ?
Also if the size is 5,5,5 i want to avoid overlapping but when the size is 30,30,30 or more then 30 it's even a bigger problem.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnObjects : MonoBehaviour
{
public int numberOfObjects;
private int currentObjects = 1;
public GameObject objectToPlace;
private int wallsLengthX;
private int wallsLengthZ;
private int wallsPosX;
private int wallsPosZ;
void Start()
{
var wi = GetComponent<WallsTest>();
wallsLengthX = (int)wi.lengthX;
wallsLengthZ = (int)wi.lengthZ;
wallsPosX = (int)wi.wallsStartPosition.x;
wallsPosZ = (int)wi.wallsStartPosition.z;
}
// Update is called once per frame
void Update()
{
GenerateRandomObjects();
}
public void GenerateRandomObjects()
{
if (currentObjects <= numberOfObjects)
{
float posx = UnityEngine.Random.Range(wallsPosX, wallsPosX + wallsLengthX);
float posz = UnityEngine.Random.Range(wallsPosZ, wallsPosZ + wallsLengthZ);
posx += wallsLengthX * -0.5f;
posz += wallsLengthZ * -0.5f;
float posy = Terrain.activeTerrain.SampleHeight(new Vector3(posx, 0, posz));
GameObject newObject = (GameObject)Instantiate(objectToPlace, new Vector3(posx, posy, posz), Quaternion.identity);
newObject.transform.localScale = new Vector3(30, 30, 30);
newObject.name = "cube-" + currentObjects;
//check if the spawned object is in the correct place
float xleft = (wallsPosX - wallsLengthX / 2) + (newObject.transform.localScale.x / 2);
float xright = (wallsPosX + wallsLengthX / 2) - (newObject.transform.localScale.x / 2);
float zbottom = (wallsPosZ - wallsLengthZ / 2) + (newObject.transform.localScale.z / 2);
float ztop = (wallsPosZ + wallsLengthZ / 2) - (newObject.transform.localScale.z / 2);
if (newObject.transform.position.x < xleft ||
newObject.transform.position.x > xright ||
newObject.transform.position.z < zbottom ||
newObject.transform.position.z > ztop)
{
Debug.Log("Out of bounds : remove it : " + newObject.name); //not an error, just info
Destroy(newObject); //remove the game object that is out of the box
}
else
{
currentObjects += 1;
}
}
if (currentObjects == numberOfObjects)
{
Debug.Log("Generate objects complete!");
}
}
}
Your answer
Follow this Question
Related Questions
How can i create array of texture2d with variables names ? 2 Answers
How can i rotate object by pressing on key R and keep object facing to me my self ? 0 Answers
How can i spawn new gameobjects to be inside the terrain area ? 2 Answers
How can i change vertex posiiton to push it back -1 on z ? 1 Answer