ExplodingProjectile.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using UnityEngine;
  2. using System.Collections;
  3. /* THIS CODE IS JUST FOR PREVIEW AND TESTING */
  4. // Feel free to use any code and picking on it, I cannot guaratnee it will fit into your project
  5. public class ExplodingProjectile : MonoBehaviour
  6. {
  7. public GameObject impactPrefab;
  8. public GameObject explosionPrefab;
  9. public float thrust;
  10. public Rigidbody thisRigidbody;
  11. public GameObject particleKillGroup;
  12. private Collider thisCollider;
  13. public bool LookRotation = true;
  14. public bool Missile = false;
  15. public Transform missileTarget;
  16. public float projectileSpeed;
  17. public float projectileSpeedMultiplier;
  18. public bool ignorePrevRotation = false;
  19. public bool explodeOnTimer = false;
  20. public float explosionTimer;
  21. float timer;
  22. private Vector3 previousPosition;
  23. // Use this for initialization
  24. void Start()
  25. {
  26. thisRigidbody = GetComponent<Rigidbody>();
  27. if (Missile)
  28. {
  29. missileTarget = GameObject.FindWithTag("Target").transform;
  30. }
  31. thisCollider = GetComponent<Collider>();
  32. previousPosition = transform.position;
  33. }
  34. // Update is called once per frame
  35. void Update()
  36. {
  37. /* if(Input.GetButtonUp("Fire2"))
  38. {
  39. Explode();
  40. }*/
  41. timer += Time.deltaTime;
  42. if (timer >= explosionTimer && explodeOnTimer == true)
  43. {
  44. Explode();
  45. }
  46. }
  47. void FixedUpdate()
  48. {
  49. if (Missile)
  50. {
  51. projectileSpeed += projectileSpeed * projectileSpeedMultiplier;
  52. // transform.position = Vector3.MoveTowards(transform.position, missileTarget.transform.position, 0);
  53. transform.LookAt(missileTarget);
  54. thisRigidbody.AddForce(transform.forward * projectileSpeed);
  55. }
  56. if (LookRotation && timer >= 0.05f)
  57. {
  58. transform.rotation = Quaternion.LookRotation(thisRigidbody.velocity);
  59. }
  60. CheckCollision(previousPosition);
  61. previousPosition = transform.position;
  62. }
  63. void CheckCollision(Vector3 prevPos)
  64. {
  65. RaycastHit hit;
  66. Vector3 direction = transform.position - prevPos;
  67. Ray ray = new Ray(prevPos, direction);
  68. float dist = Vector3.Distance(transform.position, prevPos);
  69. if (Physics.Raycast(ray, out hit, dist))
  70. {
  71. transform.position = hit.point;
  72. Quaternion rot = Quaternion.FromToRotation(Vector3.forward, hit.normal);
  73. Vector3 pos = hit.point;
  74. Instantiate(impactPrefab, pos, rot);
  75. if (!explodeOnTimer && Missile == false)
  76. {
  77. Destroy(gameObject);
  78. }
  79. else if (Missile == true)
  80. {
  81. thisCollider.enabled = false;
  82. particleKillGroup.SetActive(false);
  83. thisRigidbody.velocity = Vector3.zero;
  84. Destroy(gameObject, 5);
  85. }
  86. }
  87. }
  88. void OnCollisionEnter(Collision collision)
  89. {
  90. if (collision.gameObject.tag != "FX")
  91. {
  92. ContactPoint contact = collision.contacts[0];
  93. Quaternion rot = Quaternion.FromToRotation(Vector3.forward, contact.normal);
  94. if (ignorePrevRotation)
  95. {
  96. rot = Quaternion.Euler(0, 0, 0);
  97. }
  98. Vector3 pos = contact.point;
  99. Instantiate(impactPrefab, pos, rot);
  100. if (!explodeOnTimer && Missile == false)
  101. {
  102. Destroy(gameObject);
  103. }
  104. else if (Missile == true)
  105. {
  106. thisCollider.enabled = false;
  107. particleKillGroup.SetActive(false);
  108. thisRigidbody.velocity = Vector3.zero;
  109. Destroy(gameObject, 5);
  110. }
  111. }
  112. }
  113. void Explode()
  114. {
  115. Instantiate(explosionPrefab, gameObject.transform.position, Quaternion.Euler(0, 0, 0));
  116. Destroy(gameObject);
  117. }
  118. }