AeroplaneAudio.cs 5.8 KB

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