- Home /
Spanwer Script only spawns when Player is close and more.
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!
Answer by Piflik · Jan 01, 2013 at 10:08 PM
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.
Your answer
Follow this Question
Related Questions
losing lives 1 Answer
Slerp Problem?: Enemy in Constant Rotation! 1 Answer
Melee range AI 1 Answer
[SOLVED] Enemy Script : Mob doesn't take damage 2 Answers
Apply Damage To Player On Collison With Specific Game Object 1 Answer