- Home /
How To avoid random spawn to spawn at previously spawned object position so that they do not overlap
This code randomly spawns objects and then destroy them after some wait. what I want to do is to make sure the random spawned object is not spawned at previously spawned object such as it spawns above it. How may I do that?
public IEnumerator Spawn () {
yield return new WaitForSeconds (2.0f);
counting = true;
while (timeLeft > 0 && gameOver==false)
{
GameObject ball = balls [Random.Range (0, balls.Length)];
Vector3 spawnPosition = new Vector3 ( Random.Range (-maxWidth, maxWidth), Random.Range (-maxHeight, maxHeight), 0.0f);
Quaternion spawnRotation = Quaternion.identity;
GameObject ballClone = (GameObject) Instantiate (ball, spawnPosition, spawnRotation);
StartCoroutine (DestroyAfterWait (ballClone));
yield return new WaitForSeconds (Random.Range (1.0f, 2.0f));
}
//yield return new WaitForSeconds (2.0f);
GameOver ();
}
ok i think what i need is Physics2D.OverlapCircle. But i have not been able to figure out how to use it.
here is what i could gather which i add before instantiating ballClone
Collider[] hitColliders = Physics2D.OverlapCircle(new Vector2(Screen.width/2,Screen.height/2),1000.0f);
foreach (Collider col in colliders)
{
// have to check here if the next spawned object is going to collide with any of the previous ones...
//if so then dont spawn there
}
Answer by netvortex_dc · Jan 01, 2015 at 04:26 PM
Put the Balls in a List and check if the spawnPosition is closer to it than the size of the ball.
using System.Collections.Generic; private List spawnedBalls = new List ();
public IEnumerator Spawn () {
yield return new WaitForSeconds (2.0f);
counting = true;
while (timeLeft > 0 && gameOver==false)
{
MakeClean();
Vector spawnPosition = GetSpawnPosition();
if(spawnPosition != Vector3.zero)
{
Quaternion spawnRotation = Quaternion.identity;
GameObject ballClone = (GameObject) Instantiate (ball, spawnPosition, spawnRotation);
spawnedPositions.Add(ballClone);
StartCoroutine (DestroyAfterWait (ballClone));
}
yield return new WaitForSeconds (Random.Range (1.0f, 2.0f));
}
//yield return new WaitForSeconds (2.0f);
GameOver ();
}
void MakeClean()
{
while(!CleanUpList())
{
CleanUpList();
}
}
bool CleanUpList()
{
foreach(GameObject ball in spawnedBalls)
{
if(ball != null)
continue;
else
{
spawnedBalls.Remove(ball);
return false;
}
}
return true;
}
Vector3 GetSpawnPosition()
{
GameObject ball = balls [Random.Range (0, balls.Length)];
Vector3 spawnPosition = new Vector3 ( Random.Range (-maxWidth, maxWidth), Random.Range (-maxHeight, maxHeight), 0.0f);
float sizeOfBall = 1.0f;
foreach(GameObject ball in spawnedBalls)
{
if(Vector3.Distance(ball.transform.position, spawnPosition) <= sizeOfBall)
{
return Vector3.zero;
}
}
return spawnPosition;
}
Code is untested but i hope you get the idea. I noticed comparing Vectors is much faster than using Physics in large projects so i started to use this approach more often in my projects. (That was in Unity4, not sure about U5). So GetSpawnPosition either returns Vector3.zero or if the position was fine a valid Vector3.
ok thank you i have read your answer and will work on it to see if it works and then come back.
Your answer
Follow this Question
Related Questions
Instantiate Random 3D Objects/platforms that dont overlap 1 Answer
avoid overlap problem when randomly instantiating 0 Answers
Why do objects keep spawning on top of one another? 0 Answers
Randomly Place Cubes In Viewport Without Overlap 1 Answer
move gameobject into a random position and the spawn an enemy 1 Answer