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