AeroplaneAudio.cs 5.8 KB

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