Spawn System

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!
Thanks

Hey, So I’ve made a system that seems to work from my tests. This assumes that you have made a prefab for your enemy and also given the prefab a tag.

Here is the code:

//Spawn Variables
var spawnPoint0 : GameObject; //Add in more or less depending on your amount of spawn locations
var spawnPoint1 : GameObject;
var spawnPoint2 : GameObject;
var spawnPoint3 : GameObject;
var spawnPoint4 : GameObject;

//Enemy Variables
var currentActive : int; //Current amount of active enemies
var enemyPrefab : GameObject; //Your enemy prefab should be dragged into here
var maxNumber : int;
var maxActive : int;

function setMaxNumber(numberMax : int, numberActive : int) //numberMax is the maximum overall, numberActive is the maximum allowed to be active
{
	maxNumber = numberMax; //Set variable to change maximum overall
	maxActive = numberActive; //Set variable to change maximum active at one time
	spawnEnemies();
}

function spawnEnemies ()
{
	var randSpawn : int;

	if(currentActive < maxActive  maxNumber > 0)
	{
		randSpawn = Random.Range(0, 5); //Change the max number to the number of sapwn locations you have
		
		switch(randSpawn)
		{
			case 0:
				Instantiate(enemyPrefab, spawnPoint0.transform.position, Quaternion.identity);
				addActive();
			break;
			case 1:
				Instantiate(enemyPrefab, spawnPoint1.transform.position, Quaternion.identity);
				addActive();
			break;
			case 2:
				Instantiate(enemyPrefab, spawnPoint2.transform.position, Quaternion.identity);
				addActive();
			break;
			case 3:
				Instantiate(enemyPrefab, spawnPoint3.transform.position, Quaternion.identity);
				addActive();
			break;
			case 4:
				Instantiate(enemyPrefab, spawnPoint4.transform.position, Quaternion.identity);
				addActive();
			break;
			//Add in more cases based on your number of spawn points, because 0 is included in random.range your end case should always be 1 less than you max variable
		}
	}
}

function addActive ()
{
	currentActive++;
	maxNumber --;
	spawnCooldown();
}

function removeActive()//Each time an enemy is killed, call this function
{
	currentActive--;
	spawnEnemies();
}

function reset()
{
	var enemies : GameObject[];
	enemies = GameObject.FindGameObjectsWithTag("EnemyTag"); //Replace this with with the tag of your prefab
	
	if(enemies != null) //Check if any enemies exist, if they do destroy them
	{
		for(var i = 0; i < enemies.length; i++)
		{
			Destroy(enemies[i].gameObject);
		}
	}
	
	maxActive = 0;	//reset variables for new wave
	maxNumber = 0;
	currentActive = 0;
}

function spawnCooldown ()//Waits 1 second before spawning another enemy
{
	var i : float;
	var rate : float;
	
	rate = 2;//Change the rate to increase/decrease
	
	while (i < 1)
	{
		i+= Time.deltaTime * rate;
		yield;
	}
	
	spawnEnemies();
}

To use this code you must take the following steps:

  • Drag and drop the code onto an object in your scene (The camera is a good choice as it is never removed)
  • Change the amount of spawn location variables to the correct number you currently have
  • Change the Random.Range maximum value to the maximum spawn locations you have
  • Change the tag in the reset function to your enemy tag
  • Drag and drop the spawn locations into the variables
  • Drag and drop your prefab into the prefab variable

In order to spawn enemies you must call the setMaxNumber function with the maximum overall and maximum active so e.g. setMaxNumber(40, 10);

You only need to call this once at the start of the wave.

In order to remove enemies you must call the removeActive() function upon an enemy death or another enemy will not spawn.

Once all the enemies have been spawned/killed and you are ready to start a new wave you should call the reset() function.

Then you can call the setMaxNumber function again, with your increased difficulty e.g. setMaxNumber(60,20);

P.S. You can change the spawnCooldown function so that enemies you spawned faster or slower, I added this so that enemies would not spawn on top of eachother.

I hope this helps with what you were looking for. :slight_smile:

Something like this is relatively simple. You should really be learning and not asking for full scripts.

Thanks for the code I am going to use this script as a base, then change it to my liking :slight_smile:

I’d stay away from hardcoded things like var spawnpoint1, var spawnpoint2… this screams out for an array.

Firstly it makes the code more generic and reusable (different levels having different numbers of spawn points all using the same spawn script) and secondly if you use an array and the random range is between 0 and the size of the array you’re not going to run into a situation where your hardcoded spawn point variables are more or less than the hardcoded number in the random range bit.