Guest User

Untitled

a guest
Jan 19th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class ObstacleSpawner : MonoBehaviour {
  6.  
  7. public GameObject[] obstacles;
  8. public List<GameObject> obstaclesToSpawn = new List<GameObject> ();
  9.  
  10. int index;
  11. public float speed;
  12.  
  13. void Awake()
  14. {
  15. InitObstacles ();
  16. }
  17.  
  18. // Use this for initialization
  19. void Start ()
  20. {
  21. StartCoroutine (SpawnRandomObstacle ());
  22. StartCoroutine (ChangeSpeed ());
  23. }
  24.  
  25. void InitObstacles()
  26. {
  27. index = 0;
  28. // Initialisiere die Hindernisse
  29. for (int i = 0; i < obstacles.Length * 3; i++)
  30. {
  31. GameObject obj = Instantiate (obstacles [index], transform.position, Quaternion.identity);
  32. obstaclesToSpawn.Add (obj);
  33. obstaclesToSpawn [i].SetActive (false);
  34. index++;
  35.  
  36. if (index == obstacles.Length) {
  37. index = 0;
  38. }
  39. }
  40. }
  41.  
  42. IEnumerator SpawnRandomObstacle()
  43. {
  44. // Warte eine gewisse Zeit
  45. yield return new WaitForSeconds (Random.Range (1.5f, 4.5f));
  46. // Aktiviere Hindernisse
  47. int index = Random.Range(0, obstaclesToSpawn.Count);
  48.  
  49. while (true) {
  50. if (!obstaclesToSpawn [index].activeInHierarchy) {
  51. obstaclesToSpawn [index].SetActive (true);
  52. obstaclesToSpawn [index].transform.position = transform.position;
  53. break;
  54. } else {
  55. index = Random.Range (0, obstaclesToSpawn.Count);
  56. }
  57. }
  58.  
  59. StartCoroutine (SpawnRandomObstacle ());
  60. }
  61.  
  62. void Update()
  63. {
  64.  
  65. }
  66.  
  67. IEnumerator ChangeSpeed()
  68. {
  69. // Neue Geschwindigkeit festlegen
  70. speed = speed - 2f;
  71. // Werte der GameObjekte in der Liste verändern
  72. foreach (GameObject myObstacle in obstaclesToSpawn)
  73. {
  74. myObstacle.GetComponent<Obstacle> ().speed = speed;
  75. }
  76. // Warte eine gewisse Zeit
  77. yield return new WaitForSeconds (10f);
  78. // Starte Methode erneut
  79. StartCoroutine (ChangeSpeed ());
  80. }
  81.  
  82. }
Add Comment
Please, Sign In to add comment