Changing AudioSource's Roll-off via scripting?

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

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.

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?

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

4 Likes

It would be brilliant if we also could get the current volume (relative to the roll off curve).

1 Like

I agree with Friduric, it would be great to be able to get the current volume relative to the listeners position!

1 Like
public AudioSource Play(AudioClip clip, float volume, float pitch)
        {
            AudioSource audioSource = this.gameObject.AddComponent<AudioSource>();

            audioSource.rolloffMode = AudioRolloffMode.Custom;
            audioSource.spatialBlend = 1;
            audioSource.maxDistance = 130;
            audioSource.spread = 1;
            audioSource.dopplerLevel = 0.5f;
            audioSource.reverbZoneMix = 1;
            audioSource.clip = clip;
            audioSource.volume = volume;
            audioSource.pitch = pitch;
            audioSource.Play ();
            Destroy (audioSource, clip.length);
            return audioSource;
        }
1 Like

@wkaj is it now possible to get the volume after rolloff in Unity 2019?

1 Like

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!

1 Like
[System.Serializable]
    public class AudioRollOff
    {
        public AudioRolloffMode rolloffMode = AudioRolloffMode.Logarithmic;

        public float distanceMin = 1;
        public float distanceMax = 500;
        public bool setCustomCurve=false;

        public AnimationCurve distanceCurve = new AnimationCurve(new Keyframe[] { new Keyframe(0, 1), new Keyframe(1, 0) });


        public void Set(AudioSource audioSource)
        {
          
            audioSource.rolloffMode = rolloffMode;
            audioSource.minDistance = distanceMin;
            audioSource.maxDistance = distanceMax;
           
            if(setCustomCurve)
                 audioSource.SetCustomCurve(AudioSourceCurveType.CustomRolloff, distanceCurve);
        }
    }
3 Likes

Excellent, thanks FlightOfOne… exactly what I was after! :smile:

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!

public void setAudioProperties()
{
    int minDistance = 1;
    int maxDistance = 5;
    // Spatial blend will give audio a 3D presence
    unityAudioSource.spatialBlend = 1;
    unityAudioSource.minDistance = minDistance;
    unityAudioSource.maxDistance = maxDistance;
    // The curve will make the audio drop to 0 if a user is too far.
    // We want to make the curve logarithmic as to simulate real world drop off.
    // We make the base of the log maxDistance so it 0's out naturally at max distance
    Keyframe[] keyframes = new Keyframe[maxDistance - minDistance];
    for(int i = 0; i < maxDistance - minDistance; i++)
    {
        int distanceFromSource = i + minDistance;
        // Multiplying by maxDistance ensures that the logarithm doesn't decrease too quickly for varied distance
        // The second keyframe entry is the volume. It starts at 1, and decreases logarithmically
        keyframes[i] = new Keyframe(distanceFromSource, 1 - Mathf.Log(i + 1, maxDistance));
    }
    AnimationCurve distanceCurve = new AnimationCurve(keyframes);
    unityAudioSource.rolloffMode = AudioRolloffMode.Custom;
    unityAudioSource.SetCustomCurve(AudioSourceCurveType.CustomRolloff, distanceCurve);
}
1 Like