RobotExplosion.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class RobotExplosion : MonoBehaviour
  5. {
  6. [Header("爆炸特效")]
  7. public GameObject explosionEffect; // 爆炸特效预制体
  8. public float effectDuration = 2f; // 特效持续时间
  9. [Header("碎片设置")]
  10. public GameObject[] debrisPrefabs; // 碎片预制体数组
  11. public int debrisAmount = 20; // 碎片生成数量
  12. public float minForce = 5f; // 最小爆炸力
  13. public float maxForce = 15f; // 最大爆炸力
  14. public float torqueIntensity = 50f; // 旋转强度
  15. [Header("消失设置")]
  16. public float debrisLifeTime = 3f; // 碎片存在时间
  17. public float fadeDuration = 0.5f; // 渐隐时间
  18. private bool isExploded = false;
  19. private void OnMouseDown()
  20. {
  21. if (!isExploded)
  22. {
  23. StartExplosion();
  24. isExploded = true;
  25. }
  26. }
  27. void StartExplosion()
  28. {
  29. // 生成爆炸特效
  30. if (explosionEffect != null)
  31. {
  32. GameObject effect = Instantiate(explosionEffect, transform.position, Quaternion.identity);
  33. Destroy(effect, effectDuration);
  34. }
  35. // 生成随机碎片
  36. for (int i = 0; i < debrisAmount; i++)
  37. {
  38. SpawnDebris();
  39. }
  40. // 隐藏机器人
  41. SetRobotVisible(false);
  42. }
  43. void SpawnDebris()
  44. {
  45. if (debrisPrefabs.Length == 0) return;
  46. // 随机选择碎片预制体
  47. GameObject selectedDebris = debrisPrefabs[Random.Range(0, debrisPrefabs.Length)];
  48. // 生成参数设置
  49. Vector3 spawnPos = transform.position + Random.insideUnitSphere * 0.5f;
  50. Quaternion spawnRot = Random.rotation;
  51. GameObject debris = Instantiate(selectedDebris, spawnPos, spawnRot);
  52. // 添加物理组件
  53. Rigidbody rb = debris.GetComponent<Rigidbody>();
  54. if (rb == null) rb = debris.AddComponent<Rigidbody>();
  55. // 施加随机方向力
  56. Vector3 randomDir = new Vector3(
  57. Random.Range(-1f, 1f),
  58. Random.Range(0.5f, 1f),
  59. Random.Range(-1f, 1f)
  60. ).normalized;
  61. float force = Random.Range(minForce, maxForce);
  62. rb.AddForce(randomDir * force, ForceMode.Impulse);
  63. // 添加随机旋转
  64. rb.AddTorque(
  65. Random.insideUnitSphere * torqueIntensity,
  66. ForceMode.Impulse
  67. );
  68. // 自动销毁
  69. StartCoroutine(DestroyDebris(debris));
  70. }
  71. System.Collections.IEnumerator DestroyDebris(GameObject debris)
  72. {
  73. yield return new WaitForSeconds(debrisLifeTime - fadeDuration);
  74. // 渐隐效果
  75. Renderer[] renderers = debris.GetComponentsInChildren<Renderer>();
  76. float timer = 0f;
  77. while (timer < fadeDuration)
  78. {
  79. timer += Time.deltaTime;
  80. float alpha = Mathf.Lerp(1, 0, timer / fadeDuration);
  81. foreach (Renderer r in renderers)
  82. {
  83. if (r.material.HasProperty("_Color"))
  84. {
  85. Color color = r.material.color;
  86. color.a = alpha;
  87. r.material.color = color;
  88. }
  89. }
  90. yield return null;
  91. }
  92. Destroy(debris);
  93. }
  94. void SetRobotVisible(bool visible)
  95. {
  96. // 禁用所有渲染器和碰撞体
  97. foreach (Renderer r in GetComponentsInChildren<Renderer>())
  98. {
  99. r.enabled = visible;
  100. }
  101. foreach (Collider c in GetComponentsInChildren<Collider>())
  102. {
  103. c.enabled = visible;
  104. }
  105. }
  106. }