Yeah u need mixer groups. You are doing way too much work for something simple from what i can understand about what you want.
Music Group
- Master
- Background
- Sword
Your global music volume setting should be linked to Master which will affect both Background and Sword. Then you are free to change the volume of Background to -80db for example and raise Sword to 0db when you get the item.
Whoa, well this would've been nice to have known about two years ago lol. Thanks, I watched the video and it seemed to be doing pretty much exactly what I've coded so far.
It's 2 AM and I was going to go to bed, so very quickly:
- Do not use "max volume" values. The values you change when you get the sword should be multipliers, not caps.
- Please describe in more detail the bit about this "max value" changing its value depending on the clip.
- I was under the assumption you had different AudioSources for both the bgm and sword music. Please correct me if this is wrong.
- If by "directly" you mean "actually moving the slider in the Unity editor", then the answer is no. Everything I proposed is programmatical. It has to be, since all of these values change while the game is playing. The "sliders" are irrelevant as you'll be overwriting their values anyway with "audioSource.volume = x".
- The AudioSource.volume values are only set as a result of calculations involving the other four variables. Don't think of them as variables, or if you do, think of them as two additional variables that you will be setting according to the multiplications described above.
- The solution you propose later on seems like a workaround rather than a general solution for your issue. You will run into problems again whenever a case like this arises again.
- I had no knowledge of Music Groups so you might want to try these instead (indeed, they are the exact same solution I proposed, abstracted into a general Unity system).
That's okay, it's 3am here, so maybe I'm not explaining it very well either :)
By max volume, I mean the actual volume on the AudioSource component; the AudioManager has two of these on a couple of children, so I can crossfade two tracks at the same time, while the sword has its own AudioSource. The min/max volume values on each AudioSource is of course set to be between 0 and 1 by engine defaults, I'm NOT touching that value. What I mean is that I manually set each AudioSource to be playing at for instance 0.15 (out of those 0 to 1) on most tracks because otherwise the tracks are way too loud, because they are random audioclips from Google basically, so there's no common ground between their inherent audio levels. I don't know how to remaster them (normalize?) nor do I think I have legal permission to do so.
I'm not touching the components' volume caps, the values are changed by changing the musicVolume multiplier etc, yeah. The initial volume value I set technically stays the same regardless. My code looks pretty much exactly as your previous multiplication example. I think we might be talking around each other here haha. My code is basically this:
Step 1: Bind the correct AudioClip to the correct AudioSource on the AudioManager, depending on what level has been loaded.
Step 2: Each music track has a predetermined float value set to 0.15, intended to be used as the target maxVolume allowed.
Step 3: Run the function PlayMusic(AudioCliptoPlay, ThePredeterminedAudioSource.volumeValue, AFadeDurationValue).
Step 4: This function binds the clip mentioned to the correct AudioSource child, and starts a CoroutineCrossfader(float maxMusicVolume, float duration).
For maxMusicVolume I use ThePredeterminedAudioSource.volumeValue (the 0.15 from above) * musicVolume (the global music volume from the options menu, a regular 1.0f) * masterVolume (which is just really AudioListener.volume).
Since I don't modify the 0.15 value, it's just a float value set in the MusicManager, any music playing can never be louder than 0.15 (out of 1.0 which is Unity's max value) regardless of musicVolume * masterVolume, which is exactly as it should be :) Assuming the currently playing AudioSource has a maxVolume of 0.15 when at full music and master volumes, if I want the volume to be half of that in the end, I lower either the master or music multiplier by 50%, nothing else.
I interpreted your solution as I should be modifying the 0.15 value in some cases.