2 February 2015

Games Mechanic Enemy Spawning.

#pragma strict

//initial copy taken from Unity Learning resources @ 'http://unity3d.com/learn/tutorials/projects/survival-shooter/more-enemies'

var enemy : GameObject;                // The enemy prefab to be spawned.
var spawnTime : float = 1f;            // How long between each spawn.
var spawnPoints : Transform[];         // An array of the spawn points this enemy can spawn from.

public var minnumber : int = 1;
public var maxnumber : int = 10; 
public var enemynumber : int = 0;
var enemycount : int = 0;


function Start ()
{
    // Call the Spawn function after a delay of the spawnTime and then continue to call after the same amount of time.
    InvokeRepeating ("EnemySpawn", spawnTime, spawnTime);
    enemynumber = (Random.Range (minnumber, maxnumber));
    
}


function EnemySpawn ()
{

// Find a random index between zero and one less than the number of spawn points.
   var spawnPointIndex  = (Random.Range (0, spawnPoints.Length));
if (enemycount < enemynumber)
{
enemycount++;
       // Create an instance of the enemy prefab at the randomly selected spawn point's position and rotation.
    Instantiate (enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
    }
}

Work today was primarily on this, a script to handle spawning enemies in the arenas. Can be expanded upon later as needs be. But as is of right now it works by picking a number at random between 1-10, and in x number of seconds spawns that number of enemies at one of four positions chosen at random.

No comments:

Post a Comment