Hello, I’m currently working on a 2 player local multiplayer capture the flag game. On a map are 6 spawnpoints, point 1 till 6. Each spawnpoint can spawn a variety of items, all stored in an array. Every 20 seconds, a coroutine checks if there are 6 items in the scene. If this is not the case, new items are spawned until the itemcount is 6. If a spawnpoint is available or not is stored in a boolean array.
However, the code I’m currently using causes the spawnpoints to be checked in a specific order, from left to right. I want the objects to spawn at a random spawnpoint, which is available (false bool in array).
Otherwise, you can predict where a new item will be spawned. How should I do this?
IEnumerator NewItems()
{
float timer = 20f;
while (timer >= 0f)
{
timer -= Time.deltaTime;
yield return null;
}
for (int i = 6; i > ItemCount;)
{
if (SpawnPoints[0] == false)
{
int randomItem = Random.Range(0, 5);
Instantiate(randomLevelStartItems[randomItem], Point1.position, Point1.rotation);
SpawnPoints[0] = true;
ItemCount++;
}
else if (SpawnPoints[1] == false)
{
int randomItem = Random.Range(0, 5);
Instantiate(randomLevelStartItems[randomItem], Point2.position, Point2.rotation);
SpawnPoints[1] = true;
ItemCount++;
}
else if (SpawnPoints[2] == false)
{
int randomItem = Random.Range(0, 5);
Instantiate(randomLevelStartItems[randomItem], Point3.position, Point3.rotation);
SpawnPoints[2] = true;
ItemCount++;
}
else if (SpawnPoints[2] == false)
{
int randomItem = Random.Range(0, 5);
Instantiate(randomLevelStartItems[randomItem], Point3.position, Point3.rotation);
SpawnPoints[2] = true;
ItemCount++;
}
else if (SpawnPoints[3] == false)
{
int randomItem = Random.Range(0, 5);
Instantiate(randomLevelStartItems[randomItem], Point4.position, Point4.rotation);
SpawnPoints[3] = true;
ItemCount++;
}
else if (SpawnPoints[4] == false)
{
int randomItem = Random.Range(0, 5);
Instantiate(randomLevelStartItems[randomItem], Point5.position, Point5.rotation);
SpawnPoints[4] = true;
ItemCount++;
}
else if (SpawnPoints[5] == false)
{
int randomItem = Random.Range(0, 5);
Instantiate(randomLevelStartItems[randomItem], Point6.position, Point6.rotation);
SpawnPoints[5] = true;
ItemCount++;
}
}