Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class SpawnEnemies : MonoBehaviour {
- [Range(0,30)]
- public float spawnInterval = 5;
- public int wave = 1;
- public float gameTime;
- public GameObject enemyPrefab;
- public Transform[] spawn_spots;
- private bool canSpawn = true;
- private float currentTime;
- // Use this for initialization
- void Start () {
- StartCoroutine("GameTimer");
- }
- IEnumerator GameTimer ()
- {
- yield return new WaitForSeconds(3.0f);
- canSpawn = true;
- StartCoroutine("Spawn");
- while(currentTime < gameTime)
- {
- currentTime += Time.deltaTime;
- yield return new WaitForEndOfFrame();
- }
- canSpawn = false;
- NewWave ();
- }
- IEnumerator Spawn ()
- {
- while(canSpawn)
- {
- int spawnPointIndex = Random.Range(0, spawn_spots.Length);
- Instantiate(enemyPrefab, spawn_spots[spawnPointIndex].position, spawn_spots[spawnPointIndex].rotation);
- yield return new WaitForSeconds(spawnInterval);
- }
- }
- void NewWave ()
- {
- wave++;
- spawnInterval += 5f;
- currentTime = 0;
- StartCoroutine("GameTimer");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment