ETFXProjectileScript.cs 3.6 KB

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