1/2/17

How to make a Enemy Spawn system



To iniciate with this, first we must have created the prefabs of our enemys and the scene.

Create a new Empty Object, to this call us spawn (for example)


Create the spawn point with Empty Object, and call us with diferents names to  not to confuse us. And all this spawn points we put inside the main (spawn).



In spawn we put this script:


var enemysPref: GameObject[]; //the prefabs of the enemies to spawning
var pointSpawn : GameObject[]; //all the spawn points

private var enem : int;
private var lug : int;

private var quantity : int;
var cantid : int; //this is the number of enemys to spawn
private var tiempo : float;

var minMax : float[];

var dest : boolean; // if you like infinite spawn, put this boolean in false, else put in true

function Start () {
enem = enemysPref.length;
lug = pointSpawn.length;
quantity = cantid;
}

function Update () {
tiempo -= Time.deltaTime;

if(tiempo <= 0){
if(quantity > 0){
var a = Random.Range(0, lug);
var b = Random.Range(0, enem);
Instantiate(enemysPref[b], pointSpawn[a].transform.position, pointSpawn[a].transform.rotation);
quantity --;
}else{
if(dest == true){
Destroy(this.gameObject);
}else{
quantity = cantid;
//this.gameObject.SetActive(false);
}

}
tiempo = Random.Range(minMax[0], minMax[1]); // the time between respawn of enemys is random, change you to like
}
}

/* //this is in case of you like destroy the spawn point.

var health : int = 10;

function OnTriggerEnter(other : Collider){
        if(other.tag == "bullet"){
               health --;
               if(health <= 0){
                         Destroy(this.gameObject);
               }
        }
}*/




Then, in the inspector we will stay like this:


Start the game... Enemy spawn, ready

This script can be moddifier to make a wave system.

Any questions, do not forget to comment. If it was useful, do not forget to thank and / or share

No comments:

Post a Comment