Game Development Reference
In-Depth Information
controlled by external objects—in this case your trigger—whereas canShoot is con-
trolled internally by this script to control the time between enemy spawns.
var ZombieObjects : Transform[];
var throwForce : float;
var endTime : float;
var hitParticles : ParticleEmitter;
var player : Transform;
var invade : boolean = false;
var canShoot : boolean = false;
var startTime = 0;
function Update() {
var distFromPlayer : Vector3 = player.position - transform.position;
if(distFromPlayer.magnitude < 60 && canShoot && invade){
canShoot = false;
startTime = Time.time;
var zombies : Transform = ZombieObjects[Random.Range(0, r
ZombieObjects.length)];
var newZombie : Transform = Instantiate(zombies, transform.position, r
transform.rotation);
newZombie.name = “Zombie”;
Physics.IgnoreCollision(transform.root.collider, newZombie.collider,
true);
}
if((Time.time - startTime) > endTime) {
canShoot = true;
}
}
Figure 10.23
If you forget to
change the vari-
ables, the zombies
will come out
piled on top of one
another.
4. Add this script to the ZombieSpawner object and make sure
that the endTime variable is set to 2 . If the zombies spawn too
fast they will pile up, resulting in zombie chicken-fight for-
mations like the one in Figure 10.23. Adding proper spawn-
ing time allows the zombies to come at the player on the
ground. Figure 10.24 shows a top view of a properly working
spawner.
5. In the Inspector view for the new spawner, type 1 in the
Size entry of the Zombie Object variable, and then drag
the ProfessorZombie prefab you created earlier in the chap-
ter into the new transform object slot. Since the Zombie
Object variable is an array, you can store multiple zombies
in this spawner to be randomly generated once you
create more.
Search WWH ::




Custom Search