Advertisement
Guest User

Untitled

a guest
Aug 20th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PrefabPainter : MonoBehaviour {
  5.  
  6. public int spawnedPrefabs;
  7. public int amountOfPrefabs;
  8.  
  9. private int toSpawn;
  10. private int prefabsSpawned;
  11.  
  12. public bool spawnedAll;
  13.  
  14. public float minimumHeight;
  15. public float maximumHeight;
  16.  
  17. private float randomHeight;
  18. private float randomPositionX;
  19. private float randomPositionZ;
  20. private float randomRotationY;
  21.  
  22. private Quaternion randomRotation;
  23.  
  24. public GameObject point1;
  25. public GameObject point2;
  26. public GameObject prefabToSpawn;
  27.  
  28. private GameObject currentSpawnedObject;
  29.  
  30. public GameObject[] prefabs;
  31.  
  32. void Start () {
  33.  
  34. spawnedAll = false;
  35. prefabs = new GameObject[amountOfPrefabs];
  36. SpawnPrefabs (amountOfPrefabs);
  37.  
  38. point1.GetComponent<MeshRenderer> ().enabled = false;
  39. point2.GetComponent<MeshRenderer> ().enabled = false;
  40. gameObject.GetComponent<MeshRenderer> ().enabled = false;
  41. }
  42.  
  43. void Update () {
  44.  
  45. if (spawnedAll == true) {
  46. if (spawnedPrefabs < prefabs.Length) {
  47. toSpawn = prefabs.Length - spawnedPrefabs;
  48. Debug.Log ("Needs to Spawn " + toSpawn + " Prefabs!");
  49. SpawnPrefabs (toSpawn);
  50. spawnedAll = false;
  51. }
  52. }
  53. }
  54.  
  55. void SpawnPrefabs (int amountToSpawn) {
  56.  
  57. if (prefabsSpawned < amountToSpawn) {
  58. for (int i = 0; i < amountToSpawn; i++) {
  59.  
  60. randomPositionX = Random.Range (point1.transform.position.x - 1, point2.transform.position.x);
  61. randomPositionZ = Random.Range (point1.transform.position.z - 1, point2.transform.position.z);
  62.  
  63. randomRotationY = Random.Range (0, 360);
  64. randomRotation = Quaternion.Euler (0, randomRotationY, 0);
  65.  
  66. randomHeight = Random.Range (minimumHeight, maximumHeight);
  67.  
  68. currentSpawnedObject = (GameObject)Instantiate (prefabToSpawn);
  69.  
  70. currentSpawnedObject.transform.rotation = randomRotation;
  71. currentSpawnedObject.transform.position = new Vector3 (randomPositionX, transform.position.y + prefabToSpawn.transform.localScale.y, randomPositionZ);
  72. currentSpawnedObject.transform.localScale = new Vector3 (currentSpawnedObject.transform.localScale.x, randomHeight, currentSpawnedObject.transform.localScale.z);
  73.  
  74. if (currentSpawnedObject.tag == "corn")
  75. currentSpawnedObject.GetComponent<Corn> ().spawnedBy = gameObject;
  76.  
  77. for (int k = 0; k < prefabs.Length; k++) {
  78. if (prefabs[k] == null) {
  79. prefabs[k] = currentSpawnedObject;
  80. break;
  81. }
  82. }
  83. prefabsSpawned += 1;
  84. spawnedPrefabs += 1;
  85.  
  86. Debug.Log ("Amount To Spawn is " + amountToSpawn + ", i is " + i);
  87. if (i == amountToSpawn - 1) {
  88. Debug.Log ("Spawned All");
  89. spawnedAll = true;
  90. }
  91. }
  92. }
  93. }
  94. }
  95.  
  96. using UnityEngine;
  97. using System.Collections;
  98.  
  99. public class Corn : MonoBehaviour {
  100.  
  101. public GameObject spawnedBy;
  102.  
  103. void OnTriggerStay (Collider col) {
  104.  
  105. if (col.gameObject.tag == "corn") {
  106.  
  107. spawnedBy.GetComponent<PrefabPainter> ().spawnedPrefabs -= 1;
  108. Destroy (this.gameObject);
  109. }
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement