Il try and explain this as best i can, working in unty5, 2d, c#
i have these fires as hazards, they make a sound which is just a looping fire sound, they only play when the camera is on screen though, this is managed through a script attached to the fires which detects when the camera is onscreen, however if you have a bunch of these things and theyre all on screen the sort of double over one another as each of them is playing the sound at the same time
Ive had this issue before so i have an audio master set up to play certain sounds via a script, like my moving spike scripts which would all go up the same time, however these were for sounds that only played once
the problem im trying to figure out is how to have these fire platforms play a sound in the audio manager but only when at least one of them is on screen, and since im working with boolians when a single fire platform leaves the screen and se the bool the off, the ones on the screen set the bool back to on, resulting in the sound starting from the beggining infinatley, as they are now it only sounds normal if they are all off the screen or all on the screen
any ideas? would really appreciate any ideas around this problem
You should not use Boolean but use integer to record the status of sound on/off.
As long as “the number of fire on the screen” is set from 0 to a number larger than 0, active play. Pause sound when the number goes down to 0 from a positive number.
If I were you, I will
Add a static script to record the number of fire on the screen
Add a script on the fire to detect this fire just enters the screen or goes out of the screen.,
Add a audio source to play this sound. Use the static script above to control this audio source.
Just keep a counter of how many fires are there being shown on screen and continue playing sound only if there is at least one fire on the screen. You can then update this counter when fire arrives on or leaves the screen.
Psuedo code Just to give you an idea, it’s neither accurate nor complete.
Audio Manager:
static int firesOnScreen = 0;
void Update()
{
if(firesOnScreen > 0)
{
// Play sound only if it was not already playing otherwise just let it play.
if(!soundisPlaying)
{
PlaySound();
}
} else
{
if(soundisPlaying)
{
StopSound();
}
}
public void FireCameOnScreen()
{
firesOnScreen++;
}
public void FireLeftScreen()
{
firesOnScreen--;
}
}
Now you can access these FireCameOnScreen() and FireLeftScreen() methods as you were using your boolean.