Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3. using Random = UnityEngine.Random;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7.  
  8. public class CloneObjects : MonoBehaviour
  9. {
  10. public GameObject ObjectToCreate;
  11. public int objectsHeight = 3;
  12. [HideInInspector]
  13. public GameObject[] objects;
  14.  
  15. // for tracking properties change
  16. private Vector3 _extents;
  17. private int _objectCount;
  18. private float _objectSize;
  19.  
  20. /// <summary>
  21. /// How far to place spheres randomly.
  22. /// </summary>
  23. public Vector3 Extents;
  24.  
  25. /// <summary>
  26. /// How many spheres wanted.
  27. /// </summary>
  28. public int ObjectCount;
  29. public float ObjectSize;
  30.  
  31. // Use this for initialization
  32. void Awake()
  33. {
  34. Clone();
  35. objects = GameObject.FindGameObjectsWithTag("ClonedObject");
  36. }
  37.  
  38. private void OnValidate()
  39. {
  40. // prevent wrong values to be entered
  41. Extents = new Vector3(Mathf.Max(0.0f, Extents.x), Mathf.Max(0.0f, Extents.y), Mathf.Max(0.0f, Extents.z));
  42. ObjectCount = Mathf.Max(0, ObjectCount);
  43. ObjectSize = Mathf.Max(0.0f, ObjectSize);
  44. }
  45.  
  46. private void Reset()
  47. {
  48. Extents = new Vector3(250.0f, 20.0f, 250.0f);
  49. ObjectCount = 100;
  50. ObjectSize = 20.0f;
  51. }
  52.  
  53. // Update is called once per frame
  54. void Update()
  55. {
  56.  
  57. }
  58.  
  59. private void Clone()
  60. {
  61. if (Extents == _extents && ObjectCount == _objectCount && Mathf.Approximately(ObjectSize, _objectSize))
  62. return;
  63.  
  64. // cleanup
  65. var ObjectsToDestroy = GameObject.FindGameObjectsWithTag("ClonedObject");
  66. foreach (var t in ObjectsToDestroy)
  67. {
  68. if (Application.isEditor)
  69. {
  70. DestroyImmediate(t);
  71. }
  72. else
  73. {
  74. Destroy(t);
  75. }
  76. }
  77.  
  78. var withTag = GameObject.FindWithTag("Terrain");
  79. if (withTag == null)
  80. throw new InvalidOperationException("Terrain not found");
  81.  
  82. for (var i = 0; i < ObjectCount; i++)
  83. {
  84. var o = Instantiate(ObjectToCreate);
  85. o.tag = "ClonedObject";
  86. o.transform.SetParent(base.gameObject.transform);
  87. o.transform.localScale = new Vector3(ObjectSize, Random.Range(3, 50)/*ObjectSize*/, ObjectSize);
  88.  
  89. // get random position
  90. var x = Random.Range(-Extents.x, Extents.x);
  91. var y = Extents.y; // sphere altitude relative to terrain below
  92. var z = Random.Range(-Extents.z, Extents.z);
  93.  
  94. // now send a ray down terrain to adjust Y according terrain below
  95. var height = 10000.0f; // should be higher than highest terrain altitude
  96. var origin = new Vector3(x, height, z);
  97. var ray = new Ray(origin, Vector3.down);
  98. RaycastHit hit;
  99. var maxDistance = 20000.0f;
  100. var nameToLayer = LayerMask.NameToLayer("Terrain");
  101. var layerMask = 1 << nameToLayer;
  102. if (Physics.Raycast(ray, out hit, maxDistance, layerMask))
  103. {
  104. var distance = hit.distance;
  105. y = height - distance + y; // adjust
  106. }
  107. else
  108. {
  109. Debug.LogWarning("Terrain not hit, using default height !");
  110. }
  111.  
  112. // place !
  113. o.transform.position = new Vector3(x, y + objectsHeight, z);
  114. }
  115. _extents = Extents;
  116. _objectCount = ObjectCount;
  117. _objectSize = ObjectSize;
  118. }
  119. }
  120.  
  121. Debug.LogWarning("Terrain not hit, using default height !");
  122.  
  123. Clone();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement