Changing Audio Mixer Group volume with UI slider

Too kind! But thank you. Because it has been such a surprisingly common question I since wrote a whole article on this on my website here: https://johnleonardfrench.com/articles/the-right-way-to-make-a-volume-slider-in-unity-using-logarithmic-conversion/

2 Likes

I remember your article from a few years ago:) It helped then and im glad you reposted here - a lifesaver once again.

1 Like

Must be Mathf.Log10(float f), because Mathf.Log(float f) is the natural logarithm in Unity.

To 0.0001.

5 Likes

You are correct.

1 Like

great

Thank you for the script, I was having trouble with it and also wanted to know why the calculation was like that. I read your explanation and though it didn’t make enough sense for me, thanks for this still! Have a great day everyone!

Thanks, videos are not my thing!

Sorry I’m late to the game, but I found setting the min to 0.001 to be key for my project thanks to John. Although at first, Unity wouldn’t allow me to set the value to 0.001. Every time I tried, it would reset it to 0. Because of that, it cause my music and/or SFX to go up in value rather than mute when the slider was moved all the way to the left. I just tried again, except instead of tabbing out of the min field after typing in 0.001, I clicked on the next slider and entered that value again. I rechecked both and they stayed at 0.001 instead of resetting to 0, and now the sliders mute the music and SFX as necessary. I’m not sure if anybody else has run into same problem, but in case they did, that’s what I did. Thanks again John!

Thank you!! You solved my problem :slight_smile:

https://discussions.unity.com/t/567394/23

Actually, with Mathf.Log works kinda better than with Mathf.Log10. Don’t ask me why.

2 Likes

I’d like to add to that, Mathf.Log works just fine :slight_smile:

Not fully correctly explanation.
It’s not related with that, But proposed solution will work (“made slider more comfortable”).

Initially slider used the decibel values, which graduated on slider at constant linear factor ((max - min) values / size of slider, in your e.g. 0 and -80 db).
When you change slider values to [0.0001; 1] you change nothing, except you begin use for SetFloat the value Mathf.Log10(sliderValue) * 20. Therefore the volume values will be in range from 0 to -80db. (if you set slider values to [0.01; 1] the range will be from 0 to -40db)

In this case you just change the scale of the slider from linear scale (values placed with one step) to a something in logarithmic scale.
It can be “Mathf.Log10(sliderValue) * 20” or “* 30” or “*19”. It’s no important.
To be honest the values on slider less than 0.01, doesn’t have any sense because can’t be set precisely values less than 0.01

The main important point is select the demanded range of volume values which you want set by a slider.
Commonly useful values in range from 0 to -40db (and have separated sliders for master volume and other volumes), in this case you can simply set slide min/max values like -40/0 and always will work the same as with “logarithmic scale”.

Note.
If you use the masterMixer.SetFloat() with “Mathf.Log10(sliderValue) * 20” don’t forget when you set initial values of slider by masterMixer.GetFloat() use the correct value for sliderValue = Mathf.Pow(10f, Volume / 20)

1 Like
// Summary:
//     Returns the natural (base e) logarithm of a specified number.
//
public static extern double Log(double d);

// Summary:
//     Returns the base 10 logarithm of a specified number.
//
public static extern double Log10(double d);

Log(x) = Log(10) * Log10(x) ~ 2.3 * Log10(x) (difference only in multiplier)
8685504--1171215--graph.jpg

Thank you!

1 Like

Thank you very much!

That is great, but I found out thanks to this tutorial:
https://www.youtube.com/watch?v=pbuJUaO-wpY
that using Log10() instead of Log() yields more accurate results.
So use Log10() instead.

Well, care to edit the original message so people stop copying garbage?

Yeah, let’s slap random functions that are slightly similar to the correct ones and then wonder why the game is full of bugs. :roll_eyes: