-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAttackerSpawner.cs
36 lines (29 loc) · 1019 Bytes
/
AttackerSpawner.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using UnityEngine;
using System.Collections;
public class AttackerSpawner : MonoBehaviour {
public GameObject[] attackerPrefabArray;
// Update is called once per frame
void Update () {
foreach(GameObject spawnedAttacker in attackerPrefabArray){
if(isTimeToSpawn(spawnedAttacker)){
Spawn(spawnedAttacker);
}
}
}
bool isTimeToSpawn(GameObject attackerGameObject){
Attacker attacker = attackerGameObject.GetComponent<Attacker>();
float meanSpawnDelay = attacker.seenEverySecs;
float spawnsPerSec = 1 / meanSpawnDelay;
float threshold = spawnsPerSec * Time.deltaTime;
if(Time.deltaTime > meanSpawnDelay){
Debug.LogWarning("Spawn rate capped by frame rate");
}
// Remove if to return the bool value from the comparison
return (Random.value < threshold);
}
void Spawn(GameObject spawnAttacker){
GameObject myAttacker = Instantiate(spawnAttacker) as GameObject;
myAttacker.transform.parent = transform;
myAttacker.transform.position = transform.position;
}
}