Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  8. 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.     }
    16.     #endif
    18. public static void RealisticRolloff(this AudioSource AS)
    19.     {
    20.         var animCurve = new AnimationCurve(
    21.             new Keyframe(AS.minDistance,1f),
    22.             new Keyframe(AS.minDistance + (AS.maxDistance - AS.minDistance ) / 4f,.35f),
    23.             new Keyframe(AS.maxDistance,0f));
    25.         AS.rolloffMode = AudioRolloffMode.Custom;
    26.         animCurve.SmoothTangents(1,.025f);
    27.         AS.SetCustomCurve(AudioSourceCurveType.CustomRolloff,animCurve);
    29.         AS.dopplerLevel = 0f;
    30.         AS.spread = 60f;
    31.     }
    32. }
    mons00n likes this.