Setting a limit to how many random objects can spawn together?
First off, I just want to say I'm a beginner at this stuff so my code may not be very pretty (help if you can lol). I currently have my 5 obstacles spawning randomly in my game (think of subway surfer obstacles) and I'd like to make it so all of the obstacles cannot spawn one after another or have all 5 of them spawn right next to each other, trapping the player. At most I'd only want 3 to be able to spawn next to each other so that the other 2 obstacles can leave room for the player to avoid them. Any help is appreciated cause like I said I'm very new at this stuff and I've googled a lot and can't seem to find a direct answer.
Code (Don't laugh):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawner : MonoBehaviour {
bool Spawning = false;
public float minTime = 0.5f;
public float maxTime = 3.0f;
public Vector3 spawnPoint1;
public Vector3 spawnPoint2;
public Vector3 spawnPoint3;
public Vector3 spawnPoint4;
public Vector3 spawnPoint5;
public GameObject[] obstacles;
IEnumerator SpawnObject1(int index, float seconds)
{
float spawnDelay = Random.Range(0f, 1f);
while (true)
{
yield return new WaitForSeconds(Random.Range(minTime, maxTime));
Vector3 spawnPosition1 = new Vector3(spawnPoint1.x, spawnPoint1.y, spawnPoint1.z);
Quaternion spawnRotation1 = new Quaternion();
Instantiate(obstacles[index], spawnPosition1, spawnRotation1);
yield return new WaitForSeconds(spawnDelay);
}
}
IEnumerator SpawnObject2(int index, float seconds)
{
float spawnDelay = Random.Range(0f, 1f);
while (true)
{
yield return new WaitForSeconds(Random.Range(minTime, maxTime));
Vector3 spawnPosition2 = new Vector3(spawnPoint2.x, spawnPoint2.y, spawnPoint2.z);
Quaternion spawnRotation2 = new Quaternion();
Instantiate(obstacles[index], spawnPosition2, spawnRotation2);
yield return new WaitForSeconds(spawnDelay);
}
}
IEnumerator SpawnObject3(int index, float seconds)
{
float spawnDelay = Random.Range(0f, 1f);
while (true)
{
yield return new WaitForSeconds(Random.Range(minTime, maxTime));
Vector3 spawnPosition3 = new Vector3(spawnPoint3.x, spawnPoint3.y, spawnPoint3.z);
Quaternion spawnRotation3 = new Quaternion();
Instantiate(obstacles[index], spawnPosition3, spawnRotation3);
yield return new WaitForSeconds(spawnDelay);
}
}
IEnumerator SpawnObject4(int index, float seconds)
{
float spawnDelay = Random.Range(0f, 1f);
while (true)
{
yield return new WaitForSeconds(Random.Range(minTime, maxTime));
Vector3 spawnPosition4 = new Vector3(spawnPoint4.x, spawnPoint4.y, spawnPoint4.z);
Quaternion spawnRotation4 = new Quaternion();
Instantiate(obstacles[index], spawnPosition4, spawnRotation4);
yield return new WaitForSeconds(spawnDelay);
}
}
IEnumerator SpawnObject5(int index, float seconds)
{
float spawnDelay = Random.Range(0f, 1f);
while (true)
{
yield return new WaitForSeconds(Random.Range(minTime, maxTime));
Vector3 spawnPosition5 = new Vector3(spawnPoint5.x, spawnPoint5.y, spawnPoint5.z);
Quaternion spawnRotation5 = new Quaternion();
Instantiate(obstacles[index], spawnPosition5, spawnRotation5);
yield return new WaitForSeconds(spawnDelay);
}
}
void Update()
{
if (!Spawning)
{
Spawning = true;
int enemyIndex = Random.Range(0, obstacles.Length);
StartCoroutine(SpawnObject1(enemyIndex, Random.Range(minTime, maxTime)));
StartCoroutine(SpawnObject2(enemyIndex, Random.Range(minTime, maxTime)));
StartCoroutine(SpawnObject3(enemyIndex, Random.Range(minTime, maxTime)));
StartCoroutine(SpawnObject4(enemyIndex, Random.Range(minTime, maxTime)));
StartCoroutine(SpawnObject5(enemyIndex, Random.Range(minTime, maxTime)));
}
}
}
Your answer
Follow this Question
Related Questions
Spawning objects at runtime in vertical-scroller game 0 Answers
Trying to make a field of meteors forever, but I can tell I'm doing it horribly 0 Answers
Spawn enemies so they aren't instantiated on top of each other (C#) 2 Answers
Spawn gameObject horde, modify concentration of spawned objects. 1 Answer
Random Object Spawn 0 Answers