1 using System.Collections;
2 using
UnityEngine;
3
4 namespace
UnityStandardAssets.Vehicles.Car
5 {
6     
[RequireComponent(typeof (AudioSource))]
7     
public class WheelEffects : MonoBehaviour
8     {
9         
public Transform SkidTrailPrefab;
10         
public static Transform skidTrailsDetachedParent;
11         
public ParticleSystem skidParticles;
12         
public bool skidding { get; private set; }
13         
public bool PlayingAudio { get; private set; }
14
15
16         
private AudioSource m_AudioSource;
17         
private Transform m_SkidTrail;
18         
private WheelCollider m_WheelCollider;
19
20
21         
private void Start()
22         {
23             skidParticles = transform.root.GetComponentInChildren<ParticleSystem>();
24
25             
if (skidParticles == null)
26             {
27                 Debug.LogWarning(
" no particle system found on car to generate smoke particles");
28             }
29             
else
30             {
31                 skidParticles.Stop();
32             }
33
34             m_WheelCollider = GetComponent<WheelCollider>();
35             m_AudioSource = GetComponent<AudioSource>();
36             PlayingAudio =
false;
37
38             
if (skidTrailsDetachedParent == null)
39             {
40                 skidTrailsDetachedParent =
new GameObject("Skid Trails - Detached").transform;
41             }
42         }
43
44
45         
public void EmitTyreSmoke()
46         {
47             skidParticles.transform.position = transform.position - transform.up*m_WheelCollider.radius;
48             skidParticles.Emit(
1);
49             
if (!skidding)
50             {
51                 StartCoroutine(StartSkidTrail());
52             }
53         }
54
55
56         
public void PlayAudio()
57         {
58             m_AudioSource.Play();
59             PlayingAudio =
true;
60         }
61
62
63         
public void StopAudio()
64         {
65             m_AudioSource.Stop();
66             PlayingAudio =
false;
67         }
68
69
70         
public IEnumerator StartSkidTrail()
71         {
72             skidding =
true;
73             m_SkidTrail = Instantiate(SkidTrailPrefab);
74             
while (m_SkidTrail == null)
75             {
76                 
yield return null;
77             }
78             m_SkidTrail.parent = transform;
79             m_SkidTrail.localPosition = -Vector3.up*m_WheelCollider.radius;
80         }
81
82
83         
public void EndSkidTrail()
84         {
85             
if (!skidding)
86             {
87                 
return;
88             }
89             skidding =
false;
90             m_SkidTrail.parent = skidTrailsDetachedParent;
91             Destroy(m_SkidTrail.gameObject,
10);
92         }
93     }
94 }


Tìm kết quả nhanh