Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5.  
  6. public class CloudSpawner : MonoBehaviour {
  7.  
  8.  
  9. [SerializeField]
  10. private GameObject[] clouds;
  11.  
  12. [SerializeField]
  13. private GameObject[] collectables;
  14.  
  15. private float distanceBetweenClouds = 3f;
  16. private float minX, maxX;
  17. private float lastCloudPositionY;
  18. private float controlX;
  19.  
  20. private GameObject player;
  21.  
  22.  
  23. void Awake() {
  24. SetMinAndMaxX ();
  25. }
  26. // Use this for initialization
  27. void Start () {
  28.  
  29. }
  30.  
  31. void SetMinAndMaxX() {
  32. Vector3 bounds = Camera.main.ScreenToWorldPoint (new Vector3 (0,0,0));
  33. //this gives you the world space coordinate of the bottom left of the screen
  34. // which will be a negative number as the centre of the camera is located at 0 X axis.
  35.  
  36. maxX = -bounds.x - 0.5f; // by using - bounds we make this a positive coordinate on teh x acis eg; 3
  37. minX = bounds.x + 0.5f; // because the X coordinate returned from ScreenToWorldPoint was negative we don't need to change this
  38.  
  39. }
  40.  
  41. void Shuffle(GameObject[] arrayToShuffle) {
  42. for (int i=0; i < arrayToShuffle.Length; i++) {
  43. GameObject temp = arrayToShuffle[i];
  44. int random = Random.Range(i, arrayToShuffle.Length);
  45. arrayToShuffle[i] = arrayToShuffle[random];
  46. arrayToShuffle[random] = temp;
  47. }
  48.  
  49. }
  50.  
  51. void CreateClouds() {
  52. Shuffle (clouds);
  53. float positionY = 0f;
  54. for (int i=0; i < clouds.Length; i++) {
  55. Vector3 temp = clouds[i].transform.position;
  56. temp.y = positionY;
  57.  
  58. if(controlX == 0) {
  59. temp.x = Random.Range(0.0f, maxX);
  60. controlX = 1;
  61. } else if (controlX == 1) {
  62. temp.x = Random.Range(0.0f, maxX);
  63. controlX = 2;
  64. } else if (controlX == 2) {
  65. temp.x = Random.Range(0.0f, maxX);
  66. controlX = 3;
  67. } else if (controlX == 3) {
  68. temp.x = Random.Range(0.0f, maxX);
  69. controlX = 0;
  70. }
  71.  
  72. lastCloudPositionY = positionY;
  73. clouds[i].transform.position = temp;
  74. positionY -= distanceBetweenClouds;
  75.  
  76. }
  77.  
  78.  
  79. }
  80.  
  81.  
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement