Search Unity

Audio Slider in Unity Done RIGHT | Unity Tutorial

Discussion in 'Community Learning & Teaching' started by ChrisKurhan, Mar 1, 2022.

  1. ChrisKurhan

    ChrisKurhan

    Joined:
    Dec 28, 2015
    Posts:
    268


    Hey all! Happy #TutorialTuesday!

    This week learn how to create volume sliders, the RIGHT way! Audio sliders are an extremely common need in games, but because the AudioSource volume works fine on a scale of 0-1, it's a little confusing when you try to map a slider to multiple AudioSources with an AudioMixer. The immediate thought many of us have is "slider volume = AudioMixer volume from -80-20 or -80-0", but this is WRONG! In this video you'll see why that is wrong, and how to do it the right way, along with further reading material on why this way is "right".


    As always, the full project is available on GitHub!

    If you got value from this video, please consider liking, subscribing, and sharing to help these tutorials reach and add value to even more people. New tutorials are posted every Tuesday!
     
    vallis_unity and mgear like this.
  2. Orion

    Orion

    Joined:
    Mar 31, 2008
    Posts:
    261
    Cool tutorial :)
    A naive question: How do you deal with the slider at 0? On an exponential scale that gives me -infinite. I'm now catching 0 and set the volume to -80. Is there a more elegant solution to disable a whole mixer group?
     
    Last edited: Nov 1, 2022
  3. ChrisKurhan

    ChrisKurhan

    Joined:
    Dec 28, 2015
    Posts:
    268
    Hey thanks!
    You need to clamp the value of the slider to a really really small number, I chose 0.0001 because you're right, 0 gives you -Infinity! Re-watch this section starting at ~2:19 and it should explain how to clamp the slider!
     
  4. Orion

    Orion

    Joined:
    Mar 31, 2008
    Posts:
    261
    Ah, thanks! I must have missed that bit. That's a good fix.
    I take it there's no easy way to disable a mixer group and save performance on the audio that's playing in it, is there?
     
  5. ChrisKurhan

    ChrisKurhan

    Joined:
    Dec 28, 2015
    Posts:
    268
    Not that I know of! For simple volume controls this is a minimal performance impact that I would be really surprised if it was noticeable at all in the game.