- Home /
Adjust audio volume using an exponential curve?
I've made a slider that controls audio volume in my scene. It works fine, except that the linear fade out of the audio does not sound very good. Is there a way in script to change the way audio fades to an exponential one? Here is what I have so far:
var hSliderValue : float = 0.0;
var hthumbStyle : GUIStyle;
var hsliderStyle : GUIStyle;
function Awake(){
hSliderValue = 10.0;
}
function Update(){
AudioListener.volume = hSliderValue/10.0;
fl_time_left = hSliderValue;
}
function OnGUI () {
GUILayout.BeginArea (Rect (Screen.width/20+9, Screen.height/2+254, 376.5, 31.5));
hSliderValue = GUILayout.HorizontalSlider (hSliderValue, 0.0, 10.0,hsliderStyle,hthumbStyle);
GUILayout.EndArea ();
}
Answer by Graham-Dunnett · May 09, 2012 at 02:52 PM
Why not make the slider vary from 0 to 100. When the slider moves, convert the slider value into an integer. The use this integer to index into an array of 100 values. Use the value you get from the array as the AudioListener volume. Now the problem becomes populating the array with the volumes that you want. I guess I'd fiddle with the Mathf.Exp function with x in the range 0 to 1, and scale the return value into the range 0..1. (Exp(1) is 2.71828f). I don't know if this curve is what you want for audio volume, so you'll need to experiment. You can hardcode the values in this array in Awake().
Answer by electricsheepAmsterdam · Feb 02, 2018 at 09:06 AM
I know this is an oldy but just landed here with the same question. My solution was simple enough, like you said just make it exponential. I have a volume var scaled 0-100. I found raising it to 1.75 was a good balance giving good response along the scale but you can easily tweak that.
AudioListener.volume = Mathf.Pow (myVolume / 100f, 1.75f);
Your answer
Follow this Question
Related Questions
Linear volume control of AudioMixer groups 0 Answers
Why do AudioMixerSnapshots not crossfade properly when transitioning? 0 Answers
Fading out before load script not functioning correctly? 2 Answers
The volume does not reduce when master volume is lowered using volume slider 3 Answers
Activate sound without Pro filters 0 Answers