- Home /
How can i ramdomly intantiate gameObjects without them instantiating on top of each other?
In my code i create ramdom positions and instantiate the GameObject in that position, them i make a loop to intantiate more. The problem is that sometimes the GameObects are instantiated on top of each other. what can i do to solve that? Thanks.
Here is the code
public GameObject cube;
public Collider plane;
int size;
int nCubes;
void Start()
{
plane = GetComponent<Collider>();
Vector3 center = plane.bounds.center;
Vector3 inicio = plane.bounds.min;
Vector3 fim = plane.bounds.max;
nCubes = Random.Range(3, 6);
for (int i = 0; i < nCubes; i++)
{
Vector3 position = new Vector3(Random.Range(inicio.x, center.x), 0, Random.Range(inicio.z, fim.z));
Instantiate(cube, position, Quaternion.identity);
}
Answer by moltow · Jul 28, 2017 at 10:57 PM
You could keep a List
(or array or whatever you prefer) of positions. Then, when you instantiate your GameObject
add it's position to the List
. Then, when creating a new position, check it against the List to make sure it isn't already used.
So the position of the gameobjects will be random between the positions that i put in the array?
Sorry if I wasn't clear. The List is to keep track of Vector3 positions already in use, so when you create a new Vector3 position, you check to make sure that position doesn't already exist in the List. If it doesn't, you're free to instantiate your GameObject and then add its Vector3 position to the List. If it is the same, don't use it and generate a new Vector3 using your Random.Range technique above.
One more thing. If im spawning a tree, it have some size, and if its spawning its vector(2,0,1), and theres another tree at position vector(3,0,1), the will be kind like inside of each other. How should i solve it?
Ah yes. I see the problem. There are a number of ways you could approach this. I'm not sure I'm offering the best solution from a performance perspective, but here is one idea...
You could use the Bounds class. Ins$$anonymous$$d of having your List contain Vector3s, have it contain your Trees. From there, you can get to the Tree's Renderer component and from it, you can get at its bounds property. Then, when you create a new Vector3, you could also create a temporary Bounds instance, set it to use the newly created Vector3 as its center and then set its size to the size of the bounds of your tree. Then you can check to see if the bounds of any tree in your List intersects with the bounds you just created.
Does that make sense?
make a list with the spawn points and a bool variable and while a gameobject exist in that position turn the variable on or off so you cant instanciate more objects there, if youre position are predefined you will have no trouble of having one over the other
Your answer
Follow this Question
Related Questions
Instantiate randomly from array 1 Answer
Random.Range Spawner 1 Answer
LocalEulerAngles with random rotation doesn't work 1 Answer
Random.Range returns number in different range. 0 Answers
Random.Range always returns min value (didn't set seed) 2 Answers