Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We've updated our Terms of Service. Please read our blog post from Unity CTO and Co-Founder Joachim Ante here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

[Audio] Realistic sound rolloff tool

Discussion in 'Extensions & OnGUI' started by mitaywalle, Aug 1, 2018.

  1. mitaywalle

    mitaywalle

    Joined:
    Jul 1, 2013
    Posts:
    62
    Default behaviour of Logarithmic Rolloff curve makes it sound with non-zero volume farther then maxDistance is, and it's unacceptable for me, and i think for someone else too. At previous project I've ~250 premade and 1000 code-generated AudioSources, and I've had to correct their rolloff by hand, or privately each situation, and that was freaking time-consuming. Here is small script that help you create pretty realistic rolloff curves with few clicks.

    More over:
    - by default dopplerLevel is 1, what makes sounds strongly pitch by Camera-movement, recommend to change it closer to 0
    - by default Spread is 0, that makes sound very thin in stereo-panorama, recommend to change it closer to 30-60 degrees



    Code (CSharp):
    1. using UnityEngine;
    2. #if UNITY_EDITOR
    3. using UnityEditor;
    4. #endif
    5.  
    6.  
    7. public static class AudioSourceExtensions
    8. {
    9.     #if UNITY_EDITOR
    10.     [MenuItem("CONTEXT/AudioSource/Realistic Setup")]
    11.     public static void RalisticRolloff(MenuCommand command)
    12.     {
    13.         Undo.RecordObject(command.context,"AudioSource Realistic Setup");
    14.         ((AudioSource)command.context).RealisticRolloff();
    15.         EditorUtility.SetDirty(command.context);
    16.     }
    17.     #endif
    18.  
    19. public static void RealisticRolloff(this AudioSource AS)
    20.     {
    21.         var animCurve = new AnimationCurve(
    22.             new Keyframe(AS.minDistance,1f),
    23.             new Keyframe(AS.minDistance + (AS.maxDistance - AS.minDistance ) / 4f,.35f),
    24.             new Keyframe(AS.maxDistance,0f));
    25.      
    26.         AS.rolloffMode = AudioRolloffMode.Custom;
    27.         animCurve.SmoothTangents(1,.025f);
    28.         AS.SetCustomCurve(AudioSourceCurveType.CustomRolloff,animCurve);
    29.      
    30.         AS.dopplerLevel = 0f;
    31.         AS.spread = 60f;
    32.     }
    33. }
     
    mons00n likes this.