1 using System;
2 using
UnityEngine;
3
4 namespace
UnityStandardAssets.Vehicles.Aeroplane
5 {
6     
public class AeroplaneAudio : MonoBehaviour
7     {
8
9         
[Serializable]
10         
public class AdvancedSetttings // A class for storing the advanced options.
11         {
12             
public float engineMinDistance = 50f; // The min distance of the engine audio source.
13             
public float engineMaxDistance = 1000f; // The max distance of the engine audio source.
14             
public float engineDopplerLevel = 1f; // The doppler level of the engine audio source.
15             [Range(
0f, 1f)] public float engineMasterVolume = 0.5f; // An overall control of the engine sound volume.
16             
public float windMinDistance = 10f; // The min distance of the wind audio source.
17             
public float windMaxDistance = 100f; // The max distance of the wind audio source.
18             
public float windDopplerLevel = 1f; // The doppler level of the wind audio source.
19             [Range(
0f, 1f)] public float windMasterVolume = 0.5f; // An overall control of the wind sound volume.
20         }
21
22         [SerializeField]
private AudioClip m_EngineSound; // Looped engine sound, whose pitch and volume are affected by the plane's throttle setting.
23         [SerializeField]
private float m_EngineMinThrottlePitch = 0.4f; // Pitch of the engine sound when at minimum throttle.
24         [SerializeField]
private float m_EngineMaxThrottlePitch = 2f; // Pitch of the engine sound when at maximum throttle.
25         [SerializeField]
private float m_EngineFwdSpeedMultiplier = 0.002f; // Additional multiplier for an increase in pitch of the engine from the plane's speed.
26         [SerializeField]
private AudioClip m_WindSound; // Looped wind sound, whose pitch and volume are affected by the plane's velocity.
27         [SerializeField]
private float m_WindBasePitch = 0.2f; // starting pitch for wind (when plane is at zero speed)
28         [SerializeField]
private float m_WindSpeedPitchFactor = 0.004f; // Relative increase in pitch of the wind from the plane's speed.
29         [SerializeField]
private float m_WindMaxSpeedVolume = 100; // the speed the aircraft much reach before the wind sound reaches maximum volume.
30         [SerializeField]
private AdvancedSetttings m_AdvancedSetttings = new AdvancedSetttings();// container to make advanced settings appear as rollout in inspector
31
32         
private AudioSource m_EngineSoundSource; // Reference to the AudioSource for the engine.
33         
private AudioSource m_WindSoundSource; // Reference to the AudioSource for the wind.
34         
private AeroplaneController m_Plane; // Reference to the aeroplane controller.
35         
private Rigidbody m_Rigidbody;
36
37
38         
private void Awake()
39         {
40             
// Set up the reference to the aeroplane controller.
41             m_Plane = GetComponent<AeroplaneController>();
42             m_Rigidbody = GetComponent<Rigidbody>();
43
44
45             
// Add the audiosources and get the references.
46             m_EngineSoundSource = gameObject.AddComponent<AudioSource>();
47             m_EngineSoundSource.playOnAwake =
false;
48             m_WindSoundSource = gameObject.AddComponent<AudioSource>();
49             m_WindSoundSource.playOnAwake =
false;
50
51             
// Assign clips to the audiosources.
52             m_EngineSoundSource.clip = m_EngineSound;
53             m_WindSoundSource.clip = m_WindSound;
54
55             
// Set the parameters of the audiosources.
56             m_EngineSoundSource.minDistance = m_AdvancedSetttings.engineMinDistance;
57             m_EngineSoundSource.maxDistance = m_AdvancedSetttings.engineMaxDistance;
58             m_EngineSoundSource.loop =
true;
59             m_EngineSoundSource.dopplerLevel = m_AdvancedSetttings.engineDopplerLevel;
60
61             m_WindSoundSource.minDistance = m_AdvancedSetttings.windMinDistance;
62             m_WindSoundSource.maxDistance = m_AdvancedSetttings.windMaxDistance;
63             m_WindSoundSource.loop =
true;
64             m_WindSoundSource.dopplerLevel = m_AdvancedSetttings.windDopplerLevel;
65
66             
// call update here to set the sounds pitch and volumes before they actually play
67             Update();
68
69             
// Start the sounds playing.
70             m_EngineSoundSource.Play();
71             m_WindSoundSource.Play();
72         }
73
74
75         
private void Update()
76         {
77             
// Find what proportion of the engine's power is being used.
78             
var enginePowerProportion = Mathf.InverseLerp(0, m_Plane.MaxEnginePower, m_Plane.EnginePower);
79
80             
// Set the engine's pitch to be proportional to the engine's current power.
81             m_EngineSoundSource.pitch = Mathf.Lerp(m_EngineMinThrottlePitch, m_EngineMaxThrottlePitch, enginePowerProportion);
82
83             
// Increase the engine's pitch by an amount proportional to the aeroplane's forward speed.
84             
// (this makes the pitch increase when going into a dive!)
85             m_EngineSoundSource.pitch += m_Plane.ForwardSpeed*m_EngineFwdSpeedMultiplier;
86
87             
// Set the engine's volume to be proportional to the engine's current power.
88             m_EngineSoundSource.volume = Mathf.InverseLerp(
0, m_Plane.MaxEnginePower*m_AdvancedSetttings.engineMasterVolume,
89                                                          m_Plane.EnginePower);
90
91             
// Set the wind's pitch and volume to be proportional to the aeroplane's forward speed.
92             
float planeSpeed = m_Rigidbody.velocity.magnitude;
93             m_WindSoundSource.pitch = m_WindBasePitch + planeSpeed*m_WindSpeedPitchFactor;
94             m_WindSoundSource.volume = Mathf.InverseLerp(
0, m_WindMaxSpeedVolume, planeSpeed)*m_AdvancedSetttings.windMasterVolume;
95         }
96     }
97 }


Tìm kết quả nhanh