TouchVoxelExplosion.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace VoxelImporter
  5. {
  6. public class TouchVoxelExplosion : MonoBehaviour
  7. {
  8. public float lifeTime = 1f;
  9. public bool realTimeBake = true;
  10. public bool rebirth = true;
  11. void Update()
  12. {
  13. bool explosion = false;
  14. Vector3 position = Vector3.zero;
  15. if (Input.GetMouseButton(0))
  16. {
  17. explosion = true;
  18. position = Input.mousePosition;
  19. }
  20. if (Input.touchCount > 0)
  21. {
  22. explosion = true;
  23. position = Input.GetTouch(0).position;
  24. }
  25. if (explosion)
  26. {
  27. Ray ray = Camera.main.ScreenPointToRay(position);
  28. RaycastHit hit;
  29. if (Physics.Raycast(ray, out hit, 1000))
  30. {
  31. var colliders = Physics.OverlapSphere(hit.point, 1f);
  32. for (int i = 0; i < colliders.Length; i++)
  33. {
  34. var skinnedVoxelExplosion = colliders[i].GetComponent<VoxelSkinnedAnimationObjectExplosion>();
  35. if (skinnedVoxelExplosion != null && realTimeBake)
  36. {
  37. if (!skinnedVoxelExplosion.enabled)
  38. {
  39. var rigidbody = colliders[i].GetComponent<Rigidbody>();
  40. var rigidbodyEnabled = false;
  41. if (rigidbody != null)
  42. {
  43. rigidbodyEnabled = rigidbody.isKinematic;
  44. rigidbody.isKinematic = true;
  45. }
  46. var collider = colliders[i];
  47. collider.enabled = false;
  48. skinnedVoxelExplosion.SetExplosionCenter(skinnedVoxelExplosion.transform.worldToLocalMatrix.MultiplyPoint3x4(hit.point));
  49. var animator = collider.GetComponent<Animator>();
  50. var animatorEnabled = false;
  51. if (animator != null)
  52. {
  53. animatorEnabled = animator.enabled;
  54. animator.enabled = false;
  55. }
  56. skinnedVoxelExplosion.BakeExplosionPlay(lifeTime, () =>
  57. {
  58. if (rebirth)
  59. {
  60. skinnedVoxelExplosion.ExplosionReversePlay(lifeTime, () =>
  61. {
  62. if (animator != null) animator.enabled = animatorEnabled;
  63. if (rigidbody != null) rigidbody.isKinematic = rigidbodyEnabled;
  64. collider.enabled = true;
  65. });
  66. }
  67. else
  68. {
  69. Destroy(skinnedVoxelExplosion.gameObject);
  70. }
  71. });
  72. }
  73. }
  74. else
  75. {
  76. var voxelExplosion = colliders[i].GetComponent<VoxelBaseExplosion>();
  77. if (voxelExplosion == null) continue;
  78. if (!voxelExplosion.enabled)
  79. {
  80. var rigidbody = colliders[i].GetComponent<Rigidbody>();
  81. var rigidbodyEnabled = false;
  82. if (rigidbody != null)
  83. {
  84. rigidbodyEnabled = rigidbody.isKinematic;
  85. rigidbody.isKinematic = true;
  86. }
  87. var collider = colliders[i];
  88. collider.enabled = false;
  89. voxelExplosion.SetExplosionCenter(voxelExplosion.transform.worldToLocalMatrix.MultiplyPoint3x4(hit.point));
  90. voxelExplosion.ExplosionPlay(lifeTime, () =>
  91. {
  92. if (rebirth)
  93. {
  94. voxelExplosion.ExplosionReversePlay(lifeTime, () =>
  95. {
  96. if (rigidbody != null) rigidbody.isKinematic = rigidbodyEnabled;
  97. collider.enabled = true;
  98. });
  99. }
  100. else
  101. {
  102. Destroy(voxelExplosion.gameObject);
  103. }
  104. });
  105. }
  106. }
  107. }
  108. }
  109. }
  110. }
  111. }
  112. }