Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Changing AudioSource's Roll-off via scripting?

Discussion in 'Scripting' started by GyuJazz, Dec 14, 2010.

  1. GyuJazz

    GyuJazz

    Joined:
    Jun 23, 2010
    Posts:
    11
    Hey guys, another question about audio.

    Is there a way to change AudioSource's Roll-off values by scripting?

    I have a gameobject, which can be used either by me or someone else. The gameobject holds an audiosource and a piece of code, which controls the audiosource. I would like to keep the volume itself static, but by changing the values of Roll-Off (for example ON/OFF) I could disable the object's roll-off while holding the object, and enable it when someone else is holding on to it. This way I wouldn't hear the other player when he's miles away, while not having annoying problems with roll-off myself. Because of the 3rd person view and camera angles I would prefer to be able to disable the roll-off rather than setting it "close enough".

    I hope this short introductory was enough to explain my situation. If you have any suggestions, let me know.

    Thanks guys!

    Ile
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    You can change the source's rolloff mode using the rollOffMode property but it isn't currently possible to modify the curve from a script.
     
  3. projectskillz

    projectskillz

    Joined:
    Jun 10, 2013
    Posts:
    6
    This is a very lacking part of your audio api. One of the many reasons why this engine is difficult to use out of the box for professional use. I cant believe this is still true 5 years later. If you have a game with 100's or thousands of sounds you have to create your own solution to give each sound it own 3D sound settings.

    Not that hard to do. Why can't you guys get this done?
     
  4. wkaj

    wkaj

    Unity Technologies

    Joined:
    May 18, 2012
    Posts:
    50
    Thanks for the feedback guys,

    I've pushed the ability to set custom rolloff curves at runtime via scripts.

    You will be able to set curves for:
    - Volume rolloff
    - Spatial Blend
    - Reverb Zone Mix
    - Spread
    - Lowpass Cutoff Freq

    You should see it in the 5.2 release.

    Cheers
    W
     
  5. TruffelsAndOranges

    TruffelsAndOranges

    Joined:
    Nov 9, 2014
    Posts:
    92
    It would be brilliant if we also could get the current volume (relative to the roll off curve).
     
    M_J_V likes this.
  6. M_J_V

    M_J_V

    Joined:
    Mar 31, 2015
    Posts:
    1
    I agree with Friduric, it would be great to be able to get the current volume relative to the listeners position!
     
    Hoorza likes this.
  7. MadeThisName

    MadeThisName

    Joined:
    Mar 14, 2015
    Posts:
    115
    Code (CSharp):
    1. public AudioSource Play(AudioClip clip, float volume, float pitch)
    2.         {
    3.             AudioSource audioSource = this.gameObject.AddComponent<AudioSource>();
    4.  
    5.             audioSource.rolloffMode = AudioRolloffMode.Custom;
    6.             audioSource.spatialBlend = 1;
    7.             audioSource.maxDistance = 130;
    8.             audioSource.spread = 1;
    9.             audioSource.dopplerLevel = 0.5f;
    10.             audioSource.reverbZoneMix = 1;
    11.             audioSource.clip = clip;
    12.             audioSource.volume = volume;
    13.             audioSource.pitch = pitch;
    14.             audioSource.Play ();
    15.             Destroy (audioSource, clip.length);
    16.             return audioSource;
    17.         }
     
    Last edited: Mar 7, 2016
    CihanGurbuz likes this.
  8. hungrybelome

    hungrybelome

    Joined:
    Dec 31, 2014
    Posts:
    336
    @wkaj is it now possible to get the volume after rolloff in Unity 2019?
     
    ThatBlokeCalledJay likes this.
  9. Sckir

    Sckir

    Joined:
    Apr 13, 2017
    Posts:
    2
    Hello! @wkaj I'm in dire need of this feature as well, is there any way to get the volume value after rolloff has been calculated?

    Thank you very much!
     
    FlightOfOne likes this.
  10. FlightOfOne

    FlightOfOne

    Joined:
    Aug 1, 2014
    Posts:
    668
    Code (CSharp):
    1. [System.Serializable]
    2.     public class AudioRollOff
    3.     {
    4.         public AudioRolloffMode rolloffMode = AudioRolloffMode.Logarithmic;
    5.  
    6.         public float distanceMin = 1;
    7.         public float distanceMax = 500;
    8.         public bool setCustomCurve=false;
    9.  
    10.         public AnimationCurve distanceCurve = new AnimationCurve(new Keyframe[] { new Keyframe(0, 1), new Keyframe(1, 0) });
    11.  
    12.  
    13.         public void Set(AudioSource audioSource)
    14.         {
    15.          
    16.             audioSource.rolloffMode = rolloffMode;
    17.             audioSource.minDistance = distanceMin;
    18.             audioSource.maxDistance = distanceMax;
    19.            
    20.             if(setCustomCurve)
    21.                  audioSource.SetCustomCurve(AudioSourceCurveType.CustomRolloff, distanceCurve);
    22.         }
    23.     }
    24.  
     
    Draxidious, muzboz and Zaddo like this.
  11. muzboz

    muzboz

    Joined:
    Aug 8, 2012
    Posts:
    109
    Excellent, thanks FlightOfOne... exactly what I was after! :D
     
  12. Draxidious

    Draxidious

    Joined:
    Sep 8, 2023
    Posts:
    2
    FlightOfOne's reply was also what I was after, but I still wanted that nice logarithmic dropoff. I modified it with an equation that gives that logarithmic dropoff. I ended up not needing it however, as I realized I could make an audiosource prefab and pass that into the script (The design really isn't ideal but I stuck with it). Anyways, it has an issue where the tangents aren't smoothed out so the graph doesn't curve nicely. It should be resolvable by looping through the keyframes and using the SmoothTangents() function to make it a nice curve. I hope this is able to help someone!

    Code (CSharp):
    1. public void setAudioProperties()
    2. {
    3.     int minDistance = 1;
    4.     int maxDistance = 5;
    5.     // Spatial blend will give audio a 3D presence
    6.     unityAudioSource.spatialBlend = 1;
    7.     unityAudioSource.minDistance = minDistance;
    8.     unityAudioSource.maxDistance = maxDistance;
    9.     // The curve will make the audio drop to 0 if a user is too far.
    10.     // We want to make the curve logarithmic as to simulate real world drop off.
    11.     // We make the base of the log maxDistance so it 0's out naturally at max distance
    12.     Keyframe[] keyframes = new Keyframe[maxDistance - minDistance];
    13.     for(int i = 0; i < maxDistance - minDistance; i++)
    14.     {
    15.         int distanceFromSource = i + minDistance;
    16.         // Multiplying by maxDistance ensures that the logarithm doesn't decrease too quickly for varied distance
    17.         // The second keyframe entry is the volume. It starts at 1, and decreases logarithmically
    18.         keyframes[i] = new Keyframe(distanceFromSource, 1 - Mathf.Log(i + 1, maxDistance));
    19.     }
    20.     AnimationCurve distanceCurve = new AnimationCurve(keyframes);
    21.     unityAudioSource.rolloffMode = AudioRolloffMode.Custom;
    22.     unityAudioSource.SetCustomCurve(AudioSourceCurveType.CustomRolloff, distanceCurve);
    23. }
     
    xavi-farre likes this.