Book Image

Building an FPS Game with Unity

5 (1)
Book Image

Building an FPS Game with Unity

5 (1)

Overview of this book

Table of Contents (18 chapters)
Building an FPS Game with Unity
Credits
Foreword
About the Author
Acknowledgment
About the Reviewers
www.PacktPub.com
Preface
Index

Spawning multiple enemies at once


Taking this a little further, we can create another script that can be used to spawn multiple enemies at once.

  1. Go to the MyGame/Scripts folder, navigate to Create | New C# Script, and call it SpawnEnemiesOnTrigger. Once you've finished, double-click on the created file to open MonoDevelop.

  2. Once the file is opened, put in the following code if you're using RAIN:

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic; // List
    
    public class SpawnEnemiesOnTrigger : MonoBehaviour 
    {
      // Enemy to spawn
      public GameObject enemy;
      
      // Where to be spawned at
      public List<Transform> spawnPoints;
      
      // Has this happened already?
      private bool hasTriggered = false;
      
      void OnTriggerEnter(Collider other) 
      {
        //If the player touches the trigger, and if it hasn't 
          // been triggered before
        if(other.tag == "Player" && hasTriggered == false)
        {
          foreach(var spawnPoint in spawnPoints)
          {
            // Spawn...