Ok, so I have a plan for a spawn system, yet I have no idea how to create it. I have many ‘Spawn points’ placed around my world that I want to spawn my enemies. Each round of my game will have a set no. of enemies to spawn in over that round(this no. will increase every round to increase difficulty) however I need to limit the amount that can be alive at any given time(performance) to a set number.
EG. 40 enemies must spawn in over the round, however only 10 can be alive at any given point, so every time the enemy count drops below 10 another will spawn in until 40 zombies have been spawned. (These numbers are just examples)
Also, the spawn point chosen for each spawn must be random and obviously 2 or more enemies cannot spawn on the same spawn point at once. If somebody could put together a script to do this I’d really appreciate it, as I have no idea where to start.
Javascript only please!
var zombie:GameObject; //Set the zombie model in the inspector
var maxZombies:int; //set max zombies in this round
var maxLiveZombies:int; //set max zombies alive at once
private var zombiesThisRound:int = 0; //this is the number of zombies that have spawned this round, alive or dead
private var previousSpawnPoint:GameObject; //this is where the last zombie spawned
void Start()
{
previousSpawnPoint = GameObject.FindGameObjectsWithTag("spawnpoints")[0]; //this will break if you have no spawnpoints tagged "spawnpoints".
}
void Update()
{
while(GameObject.FindGameObjectsWithTag("zombie").length < maxLiveZombies:int || zombiesThisRound > maxZombies) //while zombies need to spawn because there aren't enough on the map and enough zombies haven't spawned yet.
{
var spawnPoints = GameObject.FindGameObjectsWithTag("spawnpoints"); //get a list of all spawn points
var spawnPoint = previousSpawnPoint; //choose a default spawn point
while(spawnPoint == previousSpawnPoint && spawnPoints.length > 1) //keep choosing a random spawn point until you choose one you haven't choosen yet
{
var spawnPoint = spawnPoints[Math.floor(Math.random() * spawnPoints.length)];
}
previousSpawnPoint = spawnPoint; //store where the zombie spawn last
var newZombie = Instantiate(zombie, spawnPoint.transform.position, spawnPoint.transform.rotation); //create the new zombie
newZombie.tag = "zombie"; //tag it as a zombie
zombiesThisRound++; //increase the number of spawned zombies.
}
}
I don’t know if this will work. It’s close to functional, though. I wrote the whole thing in the browser. Consider it a learning experience getting it to function.