Graphics Reference
In-Depth Information
OK, so there's no actual launching of anything here, just prefab instantiating. It may
not be as exciting as its name sounds, but the LaunchWave() function is at the core of the
game:
public void LaunchWave()
{
We make a quick call to CancelInvoke just in case there have been any duplicate calls
to LaunchWave(). If more than one attack wave was spawned into the same level, our
spawner would have trouble tracking how many enemies are left before the next wave and
the system wouldn't work. If there are any previous Invoke calls to LaunchWave() queued
up, they will be cancelled out by CancelInvoke:
CancelInvoke("LaunchWave");
The spawner may work in two different ways. One is to pick random items from the
spawnPrefabObjects array and spawn them; the other is to progressively go through the
array starting at zero and loop back to zero once the list is exhausted.
If the Boolean variable randomSpawning is true, the variable currentObjectNum is set
to a random number between 0 and the total number of objects in the spawnPrefabObjects
array (using Random.Range). If randomSpawning is set to false, the currentObjectNum is
incremented and a check is made to ensure that it loops back to zero when it is incremented
higher than the total number of objects in the array:
if( randomSpawning )
{
currentObjectNum= Random.Range ( 0,
totalSpawnObjects-1 );
} else {
currentObjectNum++;
// loop back to 0 when we reach the end of the
// current 'run' of things to spawn
if( currentObjectNum > totalSpawnObjects-1 )
{
// currentObjectNum=0;
// you could also implement something to tell
// game control that all waves have finished if
// you were making a game that only lasted until
// that happens
}
}
The next step is to instantiate the attack wave prefab. For this, an instance of the spawn
controller (outlined in full back in Chapter 4, Section 4.3.1) is used and its SpawnGO
(spawn game object) function. The parameters for SpawnGO are the prefab object to be
spawned, a position and a Quaternion rotation:
// create an object
GameObject tempObj=
SpawnController.Instance.SpawnGO( spawnObjectPrefabs[currentObjectNum],
Vector3.zero, Quaternion.identity );
Search WWH ::




Custom Search