- Home /
What would be a good way to not have objects spawn inside one another?
I've tried quite a few things, trying to spawn them on other heights etc, but there can be more objects spawning at the same time but I dont want them to spawn inside eachother because they arrange once they can raycast on eachother, which is impossible when they spawn inside eachother and this gets my objects stuck, what would be a good way?
Objects all spawn on the same x and z axis, how can I make sure they dont spawn in the same height? even when instantiated at the same time? spawning them goes from a global script.
Does it have to be random locations ? Can't you force them to spawn where you want them ?
If their X and Z are the same, you could just easily put a growing Y number to instantiate. just make sure that Y value is bigger than the object height
Answer by dmg0600 · Sep 23, 2014 at 08:59 AM
You could use Physics.OverlapSphere:
void InstantiateNewObject(GameObject prefab, int minimumY, int maximumY)
{
Vector3 positionToInstantiate;
Collider[] hitColliders;
do
{
positionToInstantiate = Random.Range(minimumY, maximumY);
hitColliders = Physics.OverlapSphere(positionToInstantiate, objectHeight);
} while (hitColliders.Length > 0);
Instantiate(prefab, positionToInstantiate, Quaternion.identity);
}
Take into account that maximumY won't ever be assigned as it is exclusive. Just use InstantiateNewObject whenever you want to instantiate a new object.
This seems interesting, I will have a go at it and see if it fits my needs then I will come back to this. :).
I don't know what I'm doing wrong or if the code doesn't work correctly, but my Unity freezes when I use it, also the X and Z axis are predeter$$anonymous$$ed.
Also, what does positionToInstantiate store on the x and z axis are those randomized aswell?
Right! I didn't initialize those. Use this:
void InstantiateNewObject(GameObject prefab, int $$anonymous$$imumY, int maximumY, int tries)
{
Vector3 positionToInstantiate = new Vector3(yourX, 0, yourZ);
Collider[] hitColliders;
do
{
positionToInstantiate.y = Random.Range($$anonymous$$imumY, maximumY);
hitColliders = Physics.OverlapSphere(positionToInstantiate, objectHeight);
--tries;
} while (hitColliders.Length > 0 && tries > 0);
if (tries > 0)
Instantiate(prefab, positionToInstantiate, Quaternion.identity);
}
If it freezes is because it does not find a place to instantiate. I have put a tries so if in that number of tries doesn't find a place, it won't be instantiated.
But it has to always happen, if it can't happen it has to happen a split second later? I don't quite understand what's going on.
Then put it like this:
void InstantiateNewObject(GameObject prefab, int $$anonymous$$imumY, int maximumY)
{
Vector3 positionToInstantiate = new Vector3(yourX, 0, yourZ);
Collider[] hitColliders;
do
{
positionToInstantiate.y = Random.Range($$anonymous$$imumY, maximumY);
hitColliders = Physics.OverlapSphere(positionToInstantiate, objectHeight);
} while (hitColliders.Length > 0);
Instantiate(prefab, positionToInstantiate, Quaternion.identity);
}
But be aware that if it does not find a place to Instantiate it will freeze.
Another approach is to instantiate it even if it does not find an empty place in the number of tries that you have said, if you like this better, do this:
void InstantiateNewObject(GameObject prefab, int $$anonymous$$imumY, int maximumY, int tries)
{
Vector3 positionToInstantiate = new Vector3(yourX, 0, yourZ);
Collider[] hitColliders;
do
{
positionToInstantiate.y = Random.Range($$anonymous$$imumY, maximumY);
hitColliders = Physics.OverlapSphere(positionToInstantiate, objectHeight);
--tries;
} while (hitColliders.Length > 0 && tries > 0);
Instantiate(prefab, positionToInstantiate, Quaternion.identity);
}
Answer by 767_2 · Sep 23, 2014 at 08:46 AM
you can make an array of bools to make it true if an object is instantiated on that Y value bool[] height;
spawn(int y){
if(height[y]==false){
instantiate()
height[y]=true;
}
}
BUt lets say I make an array of 0, 2, 4, 6, 8, 10. If I have 8 objects spawning at the same time and while number 10 is falling I start back at number 0, so number 0 spawns inside the falling number 10?
But I need multiple objects spawning (they will instantly fall)