Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    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. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Don't miss out on the Unite LA Keynote for the latest information from Unity! Set a reminder!
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

[Audio] Realistic sound rolloff tool

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

  1. mitaywalle


    Jul 1, 2013
    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
    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
    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));
    26.         AS.rolloffMode = AudioRolloffMode.Custom;
    27.         animCurve.SmoothTangents(1,.025f);
    28.         AS.SetCustomCurve(AudioSourceCurveType.CustomRolloff,animCurve);
    30.         AS.dopplerLevel = 0f;
    31.         AS.spread = 60f;
    32.     }
    33. }
    mons00n likes this.