I used a code to spawn 2 different prefabs .The script is working but sometimes the objects get’s overlapped ,and I can’t figure out how to avoid this overlapping.
void SpawnSpike()
{
//the bottom point of the screen
Vector2 min = Camera.main.ViewportToWorldPoint (new Vector2 (0, 0));
//this is the top-right point of the screen
Vector2 max = Camera.main.ViewportToWorldPoint (new Vector2 (1, 1));
//instantiate an enemy1
GameObject anSpikesGO = (GameObject)Instantiate (SpikesGO);
anSpikesGO.transform.position = new Vector2 (Random.Range (-min.x, -max.x), -max.y);
//Schedule when to spawn next enemy
ScheduleNextSpikeSpawn ();
}
void ScheduleNextSpikeSpawn()
{
float spawnInSeconds;
if (maxSpawnRateInSeconds > 2f)
{
//pick a number between 1 to max
spawnInSeconds = Random.Range (2f, maxSpawnRateInSeconds);
}
else
spawnInSeconds = 2f;
Invoke ("SpawnSpike", spawnInSeconds);
}