Advertisement
Guest User

Untitled

a guest
Oct 7th, 2018
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.45 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class InsectSpawner : MonoBehaviour {
  7.  
  8.     public GameObject[] insectPrefabsArray;
  9.     public float spawnCooldown = 3;
  10.     private bool isTimeToSpawn = true;
  11.  
  12.     void Start () {
  13.         if(insectPrefabsArray.Length == 0)
  14.         {
  15.             Debug.LogError("Add some insects to spawner!");
  16.         }
  17.     }
  18.    
  19.     void Update () {
  20.         if(isTimeToSpawn)
  21.         {
  22.             SpawnInsect();
  23.         }
  24.     }
  25.  
  26.     private void SpawnInsect()
  27.     {
  28.         foreach(GameObject insectPrefab in insectPrefabsArray)
  29.         {
  30.             if (insectPrefab == null) break;
  31.             //if (UnityEngine.Random.value <= (1f / 3f))
  32.             if (true)
  33.             {
  34.                 Vector3 spawnPoint = GenerateSpawnPoint();
  35.                 Instantiate(insectPrefab, spawnPoint, Quaternion.identity);
  36.                 isTimeToSpawn = false;
  37.                 break;
  38.             }
  39.         }
  40.         StartCoroutine(SpawnCooldown());
  41.     }
  42.  
  43.     private static Vector3 GenerateSpawnPoint()
  44.     {
  45.         float x = UnityEngine.Random.Range(1, 6);
  46.         float y = UnityEngine.Random.Range(6, 7);
  47.         Vector3 spawnPoint = new Vector3(x, y, 0);
  48.         return spawnPoint;
  49.     }
  50.  
  51.  
  52.     IEnumerator SpawnCooldown()
  53.     {
  54.         yield return new WaitForSeconds(spawnCooldown);
  55.         isTimeToSpawn = true;
  56.         print("Time: " + DateTime.Now);
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement