You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
2.0 KiB

  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. using UnityStandardAssets.Utility;
  5. namespace UnityStandardAssets.Effects
  6. {
  7. public class Explosive : MonoBehaviour
  8. {
  9. public Transform explosionPrefab;
  10. public float detonationImpactVelocity = 10;
  11. public float sizeMultiplier = 1;
  12. public bool reset = true;
  13. public float resetTimeDelay = 10;
  14. private bool m_Exploded;
  15. private ObjectResetter m_ObjectResetter;
  16. // implementing one method from monobehviour to ensure that the enable/disable tickbox appears in the inspector
  17. private void Start()
  18. {
  19. m_ObjectResetter = GetComponent<ObjectResetter>();
  20. }
  21. private IEnumerator OnCollisionEnter(Collision col)
  22. {
  23. if (enabled)
  24. {
  25. if (col.contacts.Length > 0)
  26. {
  27. // compare relative velocity to collision normal - so we don't explode from a fast but gentle glancing collision
  28. float velocityAlongCollisionNormal =
  29. Vector3.Project(col.relativeVelocity, col.contacts[0].normal).magnitude;
  30. if (velocityAlongCollisionNormal > detonationImpactVelocity || m_Exploded)
  31. {
  32. if (!m_Exploded)
  33. {
  34. Instantiate(explosionPrefab, col.contacts[0].point,
  35. Quaternion.LookRotation(col.contacts[0].normal));
  36. m_Exploded = true;
  37. SendMessage("Immobilize");
  38. if (reset)
  39. {
  40. m_ObjectResetter.DelayedReset(resetTimeDelay);
  41. }
  42. }
  43. }
  44. }
  45. }
  46. yield return null;
  47. }
  48. public void Reset()
  49. {
  50. m_Exploded = false;
  51. }
  52. }
  53. }