Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class InsectSpawner : MonoBehaviour {
- public GameObject[] insectPrefabsArray;
- public float spawnCooldown = 3;
- private bool isTimeToSpawn = true;
- void Start () {
- if(insectPrefabsArray.Length == 0)
- {
- Debug.LogError("Add some insects to spawner!");
- }
- }
- void Update () {
- if(isTimeToSpawn)
- {
- SpawnInsect();
- }
- }
- private void SpawnInsect()
- {
- foreach(GameObject insectPrefab in insectPrefabsArray)
- {
- if (insectPrefab == null) break;
- //if (UnityEngine.Random.value <= (1f / 3f))
- if (true)
- {
- Vector3 spawnPoint = GenerateSpawnPoint();
- Instantiate(insectPrefab, spawnPoint, Quaternion.identity);
- isTimeToSpawn = false;
- break;
- }
- }
- StartCoroutine(SpawnCooldown());
- }
- private static Vector3 GenerateSpawnPoint()
- {
- float x = UnityEngine.Random.Range(1, 6);
- float y = UnityEngine.Random.Range(6, 7);
- Vector3 spawnPoint = new Vector3(x, y, 0);
- return spawnPoint;
- }
- IEnumerator SpawnCooldown()
- {
- yield return new WaitForSeconds(spawnCooldown);
- isTimeToSpawn = true;
- print("Time: " + DateTime.Now);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement