- Home /
Decrease Volume
I have a music in my game and I want it to decrease the volume at one point, and reach to 0 so you don't hear music. How can I do that?
Answer by robertbu · Feb 22, 2013 at 07:39 PM
AudioSource.volume sets the volume for an audio source. Values range from 0 to 1.
Answer by Tarlius · Feb 22, 2013 at 07:49 PM
It sounds like you already have the music playing, so something like this should get you started. It should be pretty trivial to expand to fade the other way/etc :)
public AudioSource source; // Assign in editor/etc
public float fadeTime = 1; // fade time in seconds
public FadeSound() {
if(fadeTime == 0) {
source.volume = 0;
return;
}
StartCoroutine(_FadeSound);
}
IEnumerator _FadeSound() {
float t = fadeTime;
while (t > 0) {
yield return null;
t-= Time.deltaTime;
source.volume = t/fadeTime;
}
yield break;
}
Answer by dannyskim · Feb 22, 2013 at 07:40 PM
If you get the AudioSource that the current AudioClip is playing on, the audio source will expose the volume that you can set as a float.
Your answer
Follow this Question
Related Questions
Separate Listeners for Sound FX and Music? 0 Answers
Music volume decreases after one loop 1 Answer
Why is my backtrack's volume getting lower? 0 Answers
Audio slider for multiple scenes. 2 Answers
Inconsistent ReverbZone / AudioSource.volume behavior 2 Answers