JetParticleEffect.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using UnityEngine;
  3. namespace UnityStandardAssets.Vehicles.Aeroplane
  4. {
  5. [RequireComponent(typeof (ParticleSystem))]
  6. public class JetParticleEffect : MonoBehaviour
  7. {
  8. // this script controls the jet's exhaust particle system, controlling the
  9. // size and colour based on the jet's current throttle value.
  10. public Color minColour; // The base colour for the effect to start at
  11. private AeroplaneController m_Jet; // The jet that the particle effect is attached to
  12. private ParticleSystem m_System; // The particle system that is being controlled
  13. private float m_OriginalStartSize; // The original starting size of the particle system
  14. private float m_OriginalLifetime; // The original lifetime of the particle system
  15. private Color m_OriginalStartColor; // The original starting colout of the particle system
  16. // Use this for initialization
  17. private void Start()
  18. {
  19. // get the aeroplane from the object hierarchy
  20. m_Jet = FindAeroplaneParent();
  21. // get the particle system ( it will be on the object as we have a require component set up
  22. m_System = GetComponent<ParticleSystem>();
  23. // set the original properties from the particle system
  24. m_OriginalLifetime = m_System.main.startLifetime.constant;
  25. m_OriginalStartSize = m_System.main.startSize.constant;
  26. m_OriginalStartColor = m_System.main.startColor.color;
  27. }
  28. // Update is called once per frame
  29. private void Update()
  30. {
  31. ParticleSystem.MainModule mainModule = m_System.main;
  32. // update the particle system based on the jets throttle
  33. mainModule.startLifetime = Mathf.Lerp(0.0f, m_OriginalLifetime, m_Jet.Throttle);
  34. mainModule.startSize = Mathf.Lerp(m_OriginalStartSize*.3f, m_OriginalStartSize, m_Jet.Throttle);
  35. mainModule.startColor = Color.Lerp(minColour, m_OriginalStartColor, m_Jet.Throttle);
  36. }
  37. private AeroplaneController FindAeroplaneParent()
  38. {
  39. // get reference to the object transform
  40. var t = transform;
  41. // traverse the object hierarchy upwards to find the AeroplaneController
  42. // (since this is placed on a child object)
  43. while (t != null)
  44. {
  45. var aero = t.GetComponent<AeroplaneController>();
  46. if (aero == null)
  47. {
  48. // try next parent
  49. t = t.parent;
  50. }
  51. else
  52. {
  53. return aero;
  54. }
  55. }
  56. // controller not found!
  57. throw new Exception(" AeroplaneContoller not found in object hierarchy");
  58. }
  59. }
  60. }