I’ve got a randomizer that selects a location on a quad and then spawns an “enemy” prefab. The problem is, no matter what I do, the prefabs inevitably overlap once or twice. Sometimes they are nearly directly on top of one another. I can’t see why my use of CheckSphere is not preventing the randomizer from resetting if the location is occupied. And yes, I have swapped out the ! beforehand.
Any insight would be greatly appreciated.
{
public int maxEnemies = 5;
public static int enemyCount = 0;
public float spawnRate = 2f;
public static SpawnEnemies instance = null;
public GameObject enemy;
public Vector2 mapPosition;
public Vector2 randomSpawn;
public float timer = 0.5f;
public float stamp;
void Start()
{
mapPosition = new Vector2(0f,0f);
stamp = Time.time;
randomSpawn = mapPosition + new Vector2(Random.Range(-5f, 5f), Random.Range(-5f, 5f));
for (int i = 0; i < maxEnemies; i++)
{
MakeEnemy();
}
Spawn();
}
void MakeEnemy()
{
Instantiate(enemy, randomSpawn, Quaternion.identity);
}
public void Spawn()
{
if (enemyCount <= maxEnemies)
{
if (Time.time >= stamp)
{
MakeEnemy();
enemyCount += 1;
stamp = Time.time + timer;
}
}
}
void Randomizer()
{
randomSpawn = mapPosition + new Vector2(Random.Range(-5f, 5f), Random.Range(-5f, 5f));
}
void Update()
{
Randomizer();
while(!Physics.CheckSphere(randomSpawn,1.5f))
{
Randomizer();
}
if(enemyCount < maxEnemies)
{
Spawn();
}
}
}