Guest User

Enemy Spawner

a guest
Jun 2nd, 2018
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.14 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class SpawnEnemies : MonoBehaviour {
  6.  
  7.     [Range(0,30)]
  8.     public float spawnInterval = 5;
  9.     public int wave = 1;
  10.     public float gameTime;
  11.     public GameObject enemyPrefab;
  12.     public Transform[] spawn_spots;
  13.     private bool canSpawn = true;
  14.     private float currentTime;   
  15.  
  16.     // Use this for initialization
  17.     void Start () {
  18.  
  19.         StartCoroutine("GameTimer");       
  20.     }
  21.  
  22.     IEnumerator GameTimer ()
  23.     {
  24.         yield return new WaitForSeconds(3.0f);
  25.         canSpawn = true;
  26.         StartCoroutine("Spawn");
  27.  
  28.         while(currentTime < gameTime)
  29.         {
  30.             currentTime += Time.deltaTime;
  31.             yield return new WaitForEndOfFrame();
  32.         }
  33.  
  34.         canSpawn = false;
  35.         NewWave ();
  36.  
  37.     }
  38.  
  39.    
  40.     IEnumerator Spawn ()
  41.     {
  42.         while(canSpawn)
  43.         {          
  44.              int spawnPointIndex = Random.Range(0, spawn_spots.Length);
  45.              Instantiate(enemyPrefab, spawn_spots[spawnPointIndex].position, spawn_spots[spawnPointIndex].rotation);
  46.              yield return new WaitForSeconds(spawnInterval);
  47.         }
  48.            
  49.     }
  50.  
  51.     void NewWave ()
  52.     {
  53.         wave++;
  54.         spawnInterval += 5f;
  55.         currentTime = 0;
  56.         StartCoroutine("GameTimer");
  57.     }
  58.    
  59. }
Advertisement
Add Comment
Please, Sign In to add comment