AirControl  1.3.0
Open Source, Modular, and Extensible Flight Simulator For Deep Learning Research
AC_Airplane_Audio.cs
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 using Communicator;
5 using Unity.Collections;
6 using Commons;
7 
8 namespace AirControl{
9 
13  public class AC_Airplane_Audio : MonoBehaviour
14  {
15  #region Variables
16  [Header("Airplane Audio Properties")]
17  [Tooltip("Attach base input script to audio group; Drag and drop that script component on input")]
18  [ReadOnly][SerializeField]
19  private AC_BaseAirplane_Input input;
20  [Tooltip("Create empty Gameobject - idleSource, add audiosource to it; add audio idleSource file to audiosource; Drag and drop that game object on idleSource")]
21  [ReadOnly][SerializeField]
22  private AudioSource idleSource;
23  [Tooltip("Create empty Gameobject - fullThrottleSource, add audiosource to it; add audio fullThrottleSource file to audiosource; Drag and drop that game object on fullThrottleSource")]
24  [ReadOnly][SerializeField]
25  private AudioSource fullThrottleSource;
26  private float maxPitchValue = 1.5f;
27 
28  private float fullVolumeValue;
29  private bool isShutOff = false;
30  private float finalPitchValue;
31  // Decrease the volume gradually when engine cutoff;
32  private float fadeVolumeRate = 0.005f;
33  private bool currentEnableAudio = true;
34  #endregion
35 
36  #region Propeties
37  // property that listens to the engine shutoff script events
38  public bool ShutEngineOff
39  {
40  set{isShutOff = value;}
41  }
42 
43  #endregion
44 
45  #region Builtin Methods
46 
47  // Start is called before the first frame update
48  void Start()
49  { input = GameObject.Find(CommonFunctions.ActiveAirplane).GetComponent<AC_BaseAirplane_Input>();
50  idleSource = GameObject.Find(CommonFunctions.ActiveAirplane+"/Audio_GRP/Idle").GetComponent<AudioSource>();
51  fullThrottleSource = GameObject.Find(CommonFunctions.ActiveAirplane+"/Audio_GRP/FullThrottle").GetComponent<AudioSource>();
52  if(fullThrottleSource)
53  {
54  fullThrottleSource.volume = 0f;
55  }
56  }
57  // Update is called once per frame
58  void Update()
59  {
63  #region IOSwitch
64 
65  if(StaticAudioSchema.IsActive)
66  {
67  idleSource.enabled = StaticAudioSchema.EnableAudio;
68  fullThrottleSource.enabled = StaticAudioSchema.EnableAudio;
69  currentEnableAudio = StaticAudioSchema.EnableAudio;
70  //logging
71  Debug.unityLogger.Log(" Audio set to : "+ StaticAudioSchema.EnableAudio);
72  StaticAudioSchema.IsActive = false;
73  }
74  #endregion
75 
76  if(input)
77  {
78  if(!isShutOff)
79  {
80  HandleAudio();
81  }
82  else
83  {
84  //cutting off audio when engine cutoff
85  fullThrottleSource.volume -= fullThrottleSource.volume * fadeVolumeRate;
86  idleSource.volume -= idleSource.volume * fadeVolumeRate;
87  }
88 
89  }
90  }
91  #endregion
92 
93  #region Custom Methods
94  protected virtual void HandleAudio()
95  {
96 
97 
98  fullVolumeValue = Mathf.Lerp(0f,1f,input.StickyThrottle);
99  finalPitchValue = Mathf.Lerp(1f,maxPitchValue,input.StickyThrottle);
100  if(fullThrottleSource)
101  {
102  fullThrottleSource.volume = fullVolumeValue;
103  fullThrottleSource.pitch = finalPitchValue ;
104  }
105  }
106  #endregion
107  }
108 }
109 
AirControl.AC_BaseAirplane_Input
Base class to listen for keyboard Inputs
Definition: AC_BaseAirplane_Input.cs:12
AirControl.AC_Airplane_Audio
Setup airplane audio component
Definition: AC_Airplane_Audio.cs:13
AirControl
Definition: AirplaneSelector.cs:8
Communicator
Definition: InputHandle.cs:10
Commons
Definition: AirplaneProperties.cs:14