Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to make a sound louder the closer you get to the source and fade out the further you are away

Discussion in 'Audio & Video' started by Markeus1989, Mar 28, 2016.

  1. Markeus1989

    Markeus1989

    Joined:
    Jul 26, 2014
    Posts:
    81
    Screenshot 2016-03-27 15.39.24.png

    So I have a chimney in my 2d game. I want it so that when the player is really close to it the steam spray will be loud and once walking away it will fade out. I have a script where the sound plays once you get in distance but shuts off as soon as you're out of range. The change is too abrupt. Please Help
     

    Attached Files:

  2. BarangerHolsnyder

    BarangerHolsnyder

    Joined:
    Sep 7, 2015
    Posts:
    13
    Hi Markeus,

    Good news ! What you're trying to do already exists in Unity and is the basis of what Unity calls 3D sound. It means spatialized sound : distance, volume rolloff (what you call fade out), reverb usage, doppler and more are already there. Be careful not to confuse 3D sound with a 3D game : absolutely no link.

    I already answered something similar here, see if it helps.

    1) I'd remmond placing an AudioSource on the chimney object and tweaking it to your liking (especially the "spatialize" slider, which decides whether the sound will be spatialized by Unity or not). Do that in the editor, not by scripting, the editor's there for exactly that purpose : making your life easier and visual.

    2) Then arises the question of the AudioListener... If the camera follows the character, and depending on its actual position in the 3D space of the scene view, everything could be alright if you put, like always, an AudioListener on the camera.

    3) If this does not work like expected, tweak the AudioSource a bit more (especialy the max distance, I'd say).

    4) If the damned chimney is still wrong, you should go for a modified AudioLister : make it follow the character, while keeping the rotation coordinates equal to the ones of the camera.

    Hope this helps,

    Sacha Holsnyder

    Edit : you won't need the TriggerEnter and other boxes anymore : the audiosource manages its own "zone"
     
    Markeus1989 likes this.
  3. Markeus1989

    Markeus1989

    Joined:
    Jul 26, 2014
    Posts:
    81
    @BarangerHolsnyder Trying out what you posted right now. I'll post my results shortly, Thanks!
     
  4. Markeus1989

    Markeus1989

    Joined:
    Jul 26, 2014
    Posts:
    81
    @BarangerHolsnyder You ROCK!!!!!!!!!!!! That was awesome, it works Perfectly! Thank you very much!
     
  5. BarangerHolsnyder

    BarangerHolsnyder

    Joined:
    Sep 7, 2015
    Posts:
    13
    Cool ! Glad to help ! :)
     
    Markeus1989 likes this.