Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Spawner : MonoBehaviour {
  6.  
  7. public GameObject[] attackerPrefabArray;
  8.  
  9. // Update is called once per frame
  10. void Update () {
  11. foreach (GameObject thisAttacker in attackerPrefabArray) {
  12. if (isTimeToSpawn (thisAttacker)) {
  13. Spawn (thisAttacker);
  14. }
  15. }
  16. }
  17.  
  18. void Spawn (GameObject myGameObject){
  19. GameObject myAttacker = Instantiate (myGameObject) as GameObject;
  20. myAttacker.transform.parent = transform;
  21. myAttacker.transform.position = transform.position;
  22. }
  23.  
  24. bool isTimeToSpawn (GameObject attackerGameObject){
  25. Attacker attacker = attackerGameObject.GetComponent<Attacker> ();
  26.  
  27. float meanSpawnDelay = attacker.seenEverySeconds;
  28. float spawnsPerSecond = 1 / meanSpawnDelay;
  29.  
  30. if (Time.deltaTime > meanSpawnDelay) {
  31. Debug.LogWarning ("Spawn rate capped by frame rate");
  32. }
  33.  
  34. float threshold = spawnsPerSecond * Time.deltaTime / 5;
  35.  
  36. if (Random.value < threshold) {
  37. return true;
  38. } else {
  39. return false;
  40. }
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement