when the player is dying and/or dead, the alarm_trigger audioclip of the megaphone is jamed like that of walkman in old times.
// Find an array of the siren gameobjects.
GameObject[] sirenGameObjects = GameObject.FindGameObjectsWithTag(Tags.siren);
// Set the sirens array to have the same number of elements as there are gameobjects.
sirens = new AudioSource[sirenGameObjects.Length];
// For all the sirens allocate the audio source of the gameobjects.
for (int i = 0; i < sirens.Length; i++)
{
sirens[i] = sirenGameObjects[i].audio;
}
// For all of the sirens...
for (int i = 0; i < sirens.Length; i++)
{
// ... if alarm is triggered and the audio isn't playing, then play the audio.
if (position != resetPosition !sirens[i].isPlaying)
sirens[i].Play();
// Otherwise if the alarm isn't triggered, stop the audio.
else if (position == resetPosition)
sirens[i].Stop();
}
ps: how to debug the scripts?fox example set up breakpoints. inspect variables?
I wrapped those two code snipets in functions. And call them in Update!!the same as the tutorial.The if statements are expected to prevent calls if not needed.
But somehow it failed?Could you tell me how to fix it?
void Update()
{
// Switch the alarms and fade the music.
SwitchAlarms();
MusicFading();
}
void SwitchAlarms()
{
// Set the alarm light to be on or off.
alarm.alarmOn = position != resetPosition;
// Create a new intensity.
float newIntensity;
// If the position is not the reset position...
if (position != resetPosition)
// ... then set the new intensity to low.
newIntensity = lightLowIntensity;
else
// Otherwise set the new intensity to high.
newIntensity = lightHighIntensity;
// Fade the directional light's intensity in or out.
mainLight.intensity = Mathf.Lerp(mainLight.intensity, newIntensity, fadeSpeed * Time.deltaTime);
// For all of the sirens...
for (int i = 0; i < sirens.Length; i++)
{
// ... if alarm is triggered and the audio isn't playing, then play the audio.
if (position != resetPosition !sirens[i].isPlaying)
sirens[i].Play();
// Otherwise if the alarm isn't triggered, stop the audio.
else if (position == resetPosition)
sirens[i].Stop();
}
}
void MusicFading()
{
// If the alarm is not being triggered...
if (position != resetPosition)
{
// ... fade out the normal music...
audio.volume = Mathf.Lerp(audio.volume, 0f, musicFadeSpeed * Time.deltaTime);
// ... and fade in the panic music.
panicAudio.volume = Mathf.Lerp(panicAudio.volume, 0.8f, musicFadeSpeed * Time.deltaTime);
}
else
{
// Otherwise fade in the normal music and fade out the panic music.
audio.volume = Mathf.Lerp(audio.volume, 0.8f, musicFadeSpeed * Time.deltaTime);
panicAudio.volume = Mathf.Lerp(panicAudio.volume, 0f, musicFadeSpeed * Time.deltaTime);
}
}
}
position is public and its value would be change by other scripts.