Advertisement
Guest User

UnityTest

a guest
Feb 7th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.34 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Spawner : MonoBehaviour
  5. {
  6.     public GameObject objectToSpawn;
  7.     public float delay = 2;
  8.     public float delayAtStart = 3;
  9.     public float timer = 0;
  10.     public float randomHorizontalSpawnTreshold = 1;
  11.     public float levelMultiplier = 1;
  12.     float level;
  13.     public bool spawning = false;
  14.     Data data;
  15.     float startDelay;
  16.     // Use this for initialization
  17.     void Awake()
  18.     {
  19.         startDelay = delay;
  20.     }
  21.  
  22.     void Start()
  23.     {
  24.         timer = 0;
  25.     }
  26.  
  27.     void OnEnable()
  28.     {
  29.         delay = startDelay;
  30.         data = FindObjectOfType<Data>();
  31.         level = data.level;
  32.         delay = delay - (delay * level * levelMultiplier);
  33.     }
  34.  
  35.     // Update is called once per frame
  36.     void Update()
  37.     {
  38.         timer += Time.fixedDeltaTime;
  39.         if (timer >= delayAtStart && !spawning)
  40.         {
  41.             spawning = true;
  42.             timer = 0;
  43.         }
  44.         if (timer >= delay && spawning)
  45.         {
  46.             Instantiate(objectToSpawn, new Vector3(Random.Range(-randomHorizontalSpawnTreshold, randomHorizontalSpawnTreshold), transform.position.y, transform.position.z), Quaternion.identity);
  47.             timer = 0;
  48.         }
  49.     }
  50.  
  51.     public void ResetSpawner()
  52.     {
  53.         timer = 0;
  54.         spawning = false;
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement