ETFXProjectileScript.cs 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using UnityEngine;
  2. using System.Collections;
  3. public class ETFXProjectileScript : MonoBehaviour
  4. {
  5. public GameObject impactParticle; // Effect spawned when projectile hits a collider
  6. public GameObject projectileParticle; // Effect attached to the gameobject as child
  7. public GameObject muzzleParticle; // Effect instantly spawned when gameobject is spawned
  8. [Header("Adjust if not using Sphere Collider")]
  9. public float colliderRadius = 1f;
  10. [Range(0f, 1f)] // This is an offset that moves the impact effect slightly away from the point of impact to reduce clipping of the impact effect
  11. public float collideOffset = 0.15f;
  12. void Start()
  13. {
  14. projectileParticle = Instantiate(projectileParticle, transform.position, transform.rotation) as GameObject;
  15. projectileParticle.transform.parent = transform;
  16. if (muzzleParticle)
  17. {
  18. muzzleParticle = Instantiate(muzzleParticle, transform.position, transform.rotation) as GameObject;
  19. Destroy(muzzleParticle, 1.5f); // 2nd parameter is lifetime of effect in seconds
  20. }
  21. }
  22. void FixedUpdate()
  23. {
  24. if (GetComponent<Rigidbody>().velocity.magnitude != 0)
  25. {
  26. transform.rotation = Quaternion.LookRotation(GetComponent<Rigidbody>().velocity); // Sets rotation to look at direction of movement
  27. }
  28. RaycastHit hit;
  29. float radius; // Sets the radius of the collision detection
  30. if (transform.GetComponent<SphereCollider>())
  31. radius = transform.GetComponent<SphereCollider>().radius;
  32. else
  33. radius = colliderRadius;
  34. Vector3 direction = transform.GetComponent<Rigidbody>().velocity; // Gets the direction of the projectile, used for collision detection
  35. if (transform.GetComponent<Rigidbody>().useGravity)
  36. direction += Physics.gravity * Time.deltaTime; // Accounts for gravity if enabled
  37. direction = direction.normalized;
  38. float detectionDistance = transform.GetComponent<Rigidbody>().velocity.magnitude * Time.deltaTime; // Distance of collision detection for this frame
  39. if (Physics.SphereCast(transform.position, radius, direction, out hit, detectionDistance)) // Checks if collision will happen
  40. {
  41. transform.position = hit.point + (hit.normal * collideOffset); // Move projectile to point of collision
  42. GameObject impactP = Instantiate(impactParticle, transform.position, Quaternion.FromToRotation(Vector3.up, hit.normal)) as GameObject; // Spawns impact effect
  43. ParticleSystem[] trails = GetComponentsInChildren<ParticleSystem>(); // Gets a list of particle systems, as we need to detach the trails
  44. //Component at [0] is that of the parent i.e. this object (if there is any)
  45. for (int i = 1; i < trails.Length; i++) // Loop to cycle through found particle systems
  46. {
  47. ParticleSystem trail = trails[i];
  48. if (trail.gameObject.name.Contains("Trail"))
  49. {
  50. trail.transform.SetParent(null); // Detaches the trail from the projectile
  51. Destroy(trail.gameObject, 2f); // Removes the trail after seconds
  52. }
  53. }
  54. Destroy(projectileParticle, 3f); // Removes particle effect after delay
  55. Destroy(impactP, 3.5f); // Removes impact effect after delay
  56. Destroy(gameObject); // Removes the projectile
  57. }
  58. }
  59. }