ColliderTest.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace VoxelImporter
  5. {
  6. public class ColliderTest : MonoBehaviour
  7. {
  8. public GameObject addObject;
  9. public enum Primitive
  10. {
  11. Random = -1,
  12. Sphere = 0,
  13. Capsule = 1,
  14. Cube = 3,
  15. }
  16. public Primitive primitive = Primitive.Random;
  17. //sepalate
  18. public bool autoBirth = true;
  19. public float sepalateTimeMin = 0.5f;
  20. public float sepalateTimeMax = 1f;
  21. //random
  22. public float randomRadius = 1f;
  23. public float randomScaleMin = 0.5f;
  24. public float randomScaleMax = 1.5f;
  25. //delete
  26. public float groundY = -10f;
  27. private float timer;
  28. private float timerBeforeBirth;
  29. private List<GameObject> createList = new List<GameObject>();
  30. private int count;
  31. void Update()
  32. {
  33. if (autoBirth)
  34. {
  35. float sepalatetime = Random.Range(sepalateTimeMin, sepalateTimeMax);
  36. if (timer - timerBeforeBirth > sepalatetime)
  37. {
  38. Add();
  39. timerBeforeBirth = timer;
  40. }
  41. }
  42. for (int i = 0; i < createList.Count; i++)
  43. {
  44. var o = createList[i];
  45. if(o == null)
  46. {
  47. createList.RemoveAt(i--);
  48. continue;
  49. }
  50. if (o.transform.position.y < groundY)
  51. {
  52. Destroy(o);
  53. createList.RemoveAt(i--);
  54. continue;
  55. }
  56. }
  57. timer += Time.deltaTime;
  58. }
  59. public void Add()
  60. {
  61. GameObject o = null;
  62. if (addObject != null)
  63. {
  64. o = GameObject.Instantiate<GameObject>(addObject);
  65. }
  66. else
  67. {
  68. PrimitiveType primitiveType;
  69. if (primitive == Primitive.Random)
  70. {
  71. switch (Random.Range(0, 3))
  72. {
  73. case 0: primitiveType = PrimitiveType.Sphere; break;
  74. case 1: primitiveType = PrimitiveType.Capsule; break;
  75. default: primitiveType = PrimitiveType.Cube; break;
  76. }
  77. }
  78. else
  79. {
  80. primitiveType = (PrimitiveType)primitive;
  81. }
  82. o = GameObject.CreatePrimitive(primitiveType);
  83. }
  84. {
  85. o.layer = gameObject.layer;
  86. o.transform.SetParent(transform);
  87. o.transform.localPosition = new Vector3(Random.Range(-1f, 1f) * randomRadius, Random.Range(-1f, 1f) * randomRadius, Random.Range(-1f, 1f) * randomRadius);
  88. o.transform.localRotation = Random.rotation;
  89. float scale = Random.Range(randomScaleMin, randomScaleMax);
  90. o.transform.localScale = new Vector3(scale, scale, scale);
  91. var rigidbody = o.AddComponent<Rigidbody>();
  92. var meshFilter = o.GetComponent<MeshFilter>();
  93. if (meshFilter != null)
  94. {
  95. rigidbody.mass = scale * (meshFilter.sharedMesh.bounds.size.x * meshFilter.sharedMesh.bounds.size.y * meshFilter.sharedMesh.bounds.size.z);
  96. }
  97. else
  98. {
  99. rigidbody.mass = scale;
  100. }
  101. }
  102. {
  103. o.name += count.ToString();
  104. }
  105. createList.Add(o);
  106. count++;
  107. }
  108. }
  109. }