ZombieSpawner script

The ZombieSpawner is a simple script that randomly instantiates a zombie at one of the three ZombieSpawnPoint points every few seconds. Take the following steps to set it up:

  1. Create an empty GameObject named SpawnController and position it anywhere in the scene. I generally like to put controller scripts at 0,0,0, but since our player will be anchored to that location, feel free to place it somewhere else. Try using 0,0,-2 for this example.
  2. Add a new script to the SpawnController and name it ZombieSpawner.
  3. Move the script to our Scripts folder.
  4. Double-click the script to launch it in your editor. Edit the script to match the following:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ZombieSpawner : MonoBehaviour {
public GameObject zombiePrefab;
public float zombieSpawnFrequency; // controls how often zombies appear

// Use this for initialization
IEnumerator Start () {
while (true) {
GameObject zombie = Instantiate (zombiePrefab);
GameObject [] zombieSpawners =
GameObject.FindGameObjectsWithTag ("SpawnPoint");

zombie.transform.position = zombieSpawners
[Random.Range (0,
zombieSpawners.Length)].transform.position;
yield return new WaitForSeconds
(zombieSpawnFrequency);
}
}
}

In this script, we've created two public variables: zombiePrefab and zombieSpawnFrequency. Like the other variables, we've tried to use descriptive names to indicate their function. The zombiePrefab will be used to determine which GameObject should be spawned. Here, we are only using one type of zombie, but your project might require different prefabs being spawned from different locations.

zombieSpawnFrequency is used to determine how often the creatures are summoned from their eternal rest to seek unholy vengeance on the living. During testing, you'll want to adjust this value to meet your needs.

After assigning values to these variables, we want the script to start running. You'll notice that, instead of void Start(), we've utilized IEnumerator Start ().

Typically, a function will run to completion in a single frame. All commands are evaluated and compiled, then the screen refreshes to show the results. When a script calls for a visual change over time (such as fading the screen, animating an item, and so on) you'll probably want to use a coroutine.

A coroutine is like a function that has the ability to pause execution and return control to Unity, but also to continue where it left off in the following frame.

The IEnumerator allows us to start the spawning process, yield control back to Unity, and then pick up again where we left off in the next frame. Using this method will properly display animations and visual effects over time.

The meat of this script is within a while loop. The loop creates a new zombie instance called zombie. This instance is our previously defined prefab with all of its features and attributes, including the animations and state machine. Next, a list of spawn points is created by searching for GameObjects with tags set to SpawnPoint. A random point from that list is then selected and used as the location for the zombie instance. Once that is completed, the script waits for a number of seconds equal to the zombieSpawnFrequency variable:

  1. Save the script and return to Unity.
  2. Make sure the SpawnController is selected and note that the new Zombie Prefab field has a value of None (GameObject). Click the target selector circle next to the Zombie Prefab field and select the ZombiePrefab GameObject created in Chapter 6, Scripting Zombies for the Oculus Rift, by double-clicking. Alternatively, you could drag the GameObject from the Prefab folder to this field.
  3. Set the Zombie Spawn Frequency to five. This is only a default value, so feel free to change it after testing the scene.
  4. Save the scene, hit Play and put on the headset.

At this point, we should have an environment where the player can look around. It's probably a little too bright, so go back and turn off the Production Lighting GameObject. The mood isn't perfect, but we'll address that later. You'll also notice that our zombies are spawning at a rate of one every five seconds, but they are not moving toward the player. They are fixed at their randomly selected spawn location facing the wrong direction. We will address these next in the Zombie Controller script.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.23.130