Advertisement
Guest User

Untitled

a guest
Jan 27th, 2015
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. [RequireComponent(typeof(BoxCollider2D))]
  5. public class SpawnArea : MonoBehaviour {
  6. public GameObject spawnedPrefab;
  7. BoxCollider2D spawnArea;
  8. Vector2 maxSpawnPos;
  9.  
  10. float lastSpawnTimeS = -1;
  11. public float spawnDelayS = 5;
  12.  
  13. // Use this for initialization
  14. void Start () {
  15. spawnArea = GetComponent<BoxCollider2D>();
  16. spawnArea.enabled = false; //We don't need this to test for any collisions, just to show visual bounds info in the editor.
  17. maxSpawnPos = new Vector2(spawnArea.size.x / 2, spawnArea.size.y / 2);
  18. }
  19.  
  20. // Update is called once per frame
  21. void Update () {
  22. if (lastSpawnTimeS < 0) {
  23. lastSpawnTimeS = Time.time;
  24. print ("spawn timer fire");
  25. GameObject spawned = Instantiate(spawnedPrefab, Vector3.zero, Quaternion.identity) as GameObject;
  26. spawned.transform.parent = transform;
  27. Vector3 pos = new Vector3(Random.Range(-maxSpawnPos.x, maxSpawnPos.x), Random.Range(-maxSpawnPos.y, maxSpawnPos.y), 0);
  28. spawned.transform.localPosition = pos;
  29. } else if (lastSpawnTimeS >= 0 && Time.time - lastSpawnTimeS > spawnDelayS) {
  30. lastSpawnTimeS = -1;
  31. }
  32. }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement