Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Spawner : MonoBehaviour {
- public GameObject[] attackerPrefabArray;
- // Update is called once per frame
- void Update () {
- foreach (GameObject thisAttacker in attackerPrefabArray) {
- if (isTimeToSpawn (thisAttacker)) {
- Spawn (thisAttacker);
- }
- }
- }
- void Spawn (GameObject myGameObject){
- GameObject myAttacker = Instantiate (myGameObject) as GameObject;
- myAttacker.transform.parent = transform;
- myAttacker.transform.position = transform.position;
- }
- bool isTimeToSpawn (GameObject attackerGameObject){
- Attacker attacker = attackerGameObject.GetComponent<Attacker> ();
- float meanSpawnDelay = attacker.seenEverySeconds;
- float spawnsPerSecond = 1 / meanSpawnDelay;
- if (Time.deltaTime > meanSpawnDelay) {
- Debug.LogWarning ("Spawn rate capped by frame rate");
- }
- float threshold = spawnsPerSecond * Time.deltaTime / 5;
- if (Random.value < threshold) {
- return true;
- } else {
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement