Hi, I am making a game where after 5min enemies spawn and chase the player. Here is the code I have so far:
public var enemy : GameObject;
public var WaitTime : int;
function Start () {
yield WaitForSeconds(WaitTime);
InvokeRepeating("SpawnEnemy", 3, 3);
}
function SpawnEnemy()
{
//Instantiate the enemy prefab
var enemyClone : GameObject = Instantiate(enemy, Vector3.zero, Quaternion.identity);
}
I need help making it so that the spawner only spawns when the player is close and the spawner stops spawning after 20 enemies spawn. Any help would be great thanks!
Instead of in Start, move the Invoke repeating into OnTriggerEnter, and test if the object entering is the player (and probably an additional boolean to prevent the spawner from starting spawning again, when he already is spawning).
For the second part, implement a counter that you increment in the SpawnEnemy function. Only spawn when that number is below 20. If you want to resume spawning after one of the enemies is killed, you would need a parent variable in your enemies where the spawner stores a reference to itself, and on Destroy the enemies send a message to their parent to decrement the enemy-counter again.