PolygonProjectileScript.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace PolygonArsenal
  4. {
  5. public class PolygonProjectileScript : MonoBehaviour
  6. {
  7. public GameObject impactParticle;
  8. public GameObject projectileParticle;
  9. public GameObject muzzleParticle;
  10. public GameObject[] trailParticles;
  11. [Header("Adjust if not using Sphere Collider")]
  12. public float colliderRadius = 1f;
  13. [Range(0f, 1f)]
  14. public float collideOffset = 0.15f;
  15. void Start()
  16. {
  17. projectileParticle = Instantiate(projectileParticle, transform.position, transform.rotation) as GameObject;
  18. projectileParticle.transform.parent = transform;
  19. if (muzzleParticle)
  20. {
  21. muzzleParticle = Instantiate(muzzleParticle, transform.position, transform.rotation) as GameObject;
  22. Destroy(muzzleParticle, 1.5f); // Lifetime of muzzle effect.
  23. }
  24. }
  25. void FixedUpdate()
  26. {
  27. RaycastHit hit;
  28. float rad;
  29. if (transform.GetComponent<SphereCollider>())
  30. rad = transform.GetComponent<SphereCollider>().radius;
  31. else
  32. rad = colliderRadius;
  33. Vector3 dir = transform.GetComponent<Rigidbody>().velocity;
  34. if (transform.GetComponent<Rigidbody>().useGravity)
  35. dir += Physics.gravity * Time.deltaTime;
  36. dir = dir.normalized;
  37. float dist = transform.GetComponent<Rigidbody>().velocity.magnitude * Time.deltaTime;
  38. if (Physics.SphereCast(transform.position, rad, dir, out hit, dist))
  39. {
  40. transform.position = hit.point + (hit.normal * collideOffset);
  41. GameObject impactP = Instantiate(impactParticle, transform.position, Quaternion.FromToRotation(Vector3.up, hit.normal)) as GameObject;
  42. if (hit.transform.tag == "Destructible") // Projectile will destroy objects tagged as Destructible
  43. {
  44. Destroy(hit.transform.gameObject);
  45. }
  46. foreach (GameObject trail in trailParticles)
  47. {
  48. GameObject curTrail = transform.Find(projectileParticle.name + "/" + trail.name).gameObject;
  49. curTrail.transform.parent = null;
  50. Destroy(curTrail, 3f);
  51. }
  52. Destroy(projectileParticle, 3f);
  53. Destroy(impactP, 3.5f);
  54. Destroy(gameObject);
  55. ParticleSystem[] trails = GetComponentsInChildren<ParticleSystem>();
  56. //Component at [0] is that of the parent i.e. this object (if there is any)
  57. for (int i = 1; i < trails.Length; i++)
  58. {
  59. ParticleSystem trail = trails[i];
  60. if (trail.gameObject.name.Contains("Trail"))
  61. {
  62. trail.transform.SetParent(null);
  63. Destroy(trail.gameObject, 2f);
  64. }
  65. }
  66. }
  67. }
  68. //private bool hasCollided = false;
  69. /*void OnCollisionEnter(Collision hit)
  70. {
  71. if (!hasCollided)
  72. {
  73. hasCollided = true;
  74. impactParticle = Instantiate(impactParticle, transform.position, Quaternion.FromToRotation(Vector3.up, impactNormal)) as GameObject;
  75. if (hit.gameObject.tag == "Destructible") // Projectile will destroy objects tagged as Destructible
  76. {
  77. Destroy(hit.gameObject);
  78. }
  79. foreach (GameObject trail in trailParticles)
  80. {
  81. GameObject curTrail = transform.Find(projectileParticle.name + "/" + trail.name).gameObject;
  82. curTrail.transform.parent = null;
  83. Destroy(curTrail, 3f);
  84. }
  85. Destroy(projectileParticle, 3f);
  86. Destroy(impactParticle, 5f);
  87. Destroy(gameObject);
  88. ParticleSystem[] trails = GetComponentsInChildren<ParticleSystem>();
  89. //Component at [0] is that of the parent i.e. this object (if there is any)
  90. for (int i = 1; i < trails.Length; i++)
  91. {
  92. ParticleSystem trail = trails[i];
  93. if (trail.gameObject.name.Contains("Trail"))
  94. {
  95. trail.transform.SetParent(null);
  96. Destroy(trail.gameObject, 2f);
  97. }
  98. }
  99. }
  100. }*/
  101. }
  102. }