AudioSource volume change not working with rolloff

I don’t know if anybody has the same problem, I’m using Unity 2.6 (tried on 2.6.1, same thing) and I’m simply changing the volume of an audiosource with a script (fairly simple). Works good in the editor, but as soon as I build the game, the rolloff seems to stay at 0, no matter the distance it’s the same volume. These audiosource are on cars, so they should fade away with distance… (rolloff = 0.1) The audiosource is in the right gameobject, that move with the car and stuff…

I’ve tried updating the volume in Update(), LateUpdate(), FixedUpdate() with no luck, tried to calculate the rolloff in code with distance from camera with no luck (working in the editor, again, but no chance in the build).

Everything works if I don’t change the volume dynamically in the build… so it has nothing to do with the sound itself.

Again, works good with all methods in the editor, but not in the built

sounds like a bug… anybody can help? maybe I’m doing something wrong, but what?

I had a whole bunch of audio sources with carefully calculated rolloffs that wre fine in 2.5 but collapsed as soon as I installed 2.6 and all of them played at once in the Editor and in the builds. It was a cacophony as soon as I hit “Play”.

I reported the issue to Unity. Please do the same.

I have used a script from the Wiki I had anyway because the standard rolloff wasn’t working very well for what I wanted to do with some of my sources but now I use the script for all of them as the built in rolloff seems to have died with 2.6. But honestly the Wiki script is working better anyway.

Here you go, it’s called ModifyAudioSource. I’m sorry I don’t know how people attach scripts correctly but copy and paste into Unitron and hopefully it will work. I didn’t write it but I’m very grateful to whomever did:

// helps to automatically link the first attached instance of this script
// to an Audio Source, if no Audio Sources were present
@script RequireComponent(AudioSource)

// also helps to automatically link the Audio Listener
@script ExecuteInEditMode()

var audioSource : AudioSource;
var audioListener : Transform;
var volume : float = 1;
var minVolume : float = 0;
var maxVolume : float = 1;
var rolloffFactor : float = 1;
var rolloffThreshold : float = 1;
var ignoreDistance = false;

private var loudness : float;
private var minLoudness;
private var maxLoudness;
private var loudnessExponent = Mathf.Log(Mathf.Sqrt(10), 2);

function Volume ()
{return loudness;}

function Volume (value)
{
// volume values outside 0-1 mean nothing in Unity
volume = Mathf.Clamp01(value);

loudness = Mathf.Pow(volume, loudnessExponent);
if (audioSource ignoreDistance)
audioSource.volume = Mathf.Clamp(loudness, minLoudness, maxLoudness);
}

function MinVolume ()
{return minLoudness;}

function MinVolume (value)
{
// volume values less than 0 mean nothing in Unity
// ensure minVolume is not greater than maxVolume;
minVolume = Mathf.Clamp(value, 0, maxVolume);

minLoudness = Mathf.Pow(minVolume, loudnessExponent);
}

function MaxVolume ()
{return maxLoudness;}

function MaxVolume (value)
{
// ensure maxVolume is not less than minVolume;
// volume values greater than 1 mean nothing in Unity
maxVolume = Mathf.Clamp(value, minVolume, 1);

maxLoudness = Mathf.Pow(maxVolume, loudnessExponent);
}

private var rolloffExponent;

function RolloffFactor ()
{return rolloffExponent;}

function RolloffFactor (value)
{
// Negative values don’t make sense for this
rolloffFactor = Mathf.Max(value, 0);

rolloffExponent = Mathf.Log(.5, (1 / rolloffFactor + 1));
}

// no need for a “getter”; use rolloffThreshold directly
function RolloffThreshold (value)
{
// Negative values don’t make sense for this
rolloffThreshold = Mathf.Max(value, 0);
}

var thisTransform : Transform;

function Start()
{
// If there is only one Audio Source attached to this game object,
// and there are no other instances of this script attached,
// automatically link this script to the Audio Source.
if (GetComponents(AudioSource).Length < 2
GetComponents(typeof this).Length < 2)
audioSource = audio;

if (!audioListener)
{
var listenerComponent : Component = FindObjectOfType(AudioListener);
if (listenerComponent)
audioListener = listenerComponent.gameObject.transform;
}

// These properties are not useful;
// initializing them this way keeps this script as tidy as possible.
if (audioSource)
{
audioSource.minVolume = 0;
audioSource.maxVolume = 1;
audioSource.rolloffFactor = 0;
}

thisTransform = transform;

Volume(volume);
MinVolume(minVolume);
MaxVolume(maxVolume);
RolloffFactor(rolloffFactor);

// Change values in the Editor (during Play Mode); get results.
if (Application.isEditor Application.isPlaying)
StartCoroutine(“RefreshProperties”);
}

// Using OnDrawGizmos() would be a performance hit
// in the Editor, given several properties to update.
function RefreshProperties ()
{
while (true)
{
Volume(volume);
yield;
MinVolume(minVolume);
yield;
MaxVolume(maxVolume);
yield;
RolloffFactor(rolloffFactor);
yield;
}
}

function Update ()
{
if (Application.isPlaying audioSource audioListener (ignoreDistance == false))
{
var distanceToListener = Vector3.Distance(thisTransform.position, audioListener.position);
var rolloffBase = distanceToListener / rolloffThreshold;
var positionalVolume = Mathf.Pow(rolloffBase, rolloffExponent) * loudness;
audioSource.volume = Mathf.Clamp(positionalVolume, minLoudness, maxLoudness);
}
}

This definitely sounds like a bug as there should be no difference bt the editor and player. Can you create a, as small as possible, repro case of this(smth like 1 audiosource + 1 listener scenario)? and report it thru the the bugreporter (Help->Report Problem).

cheers!

Woo-hoo! I sincerely hope that my audio scripts on the wiki become obsolete ASAP, but I’m happy to hear that I at least helped somebody out!

Well first of all, I already had tried the audio script from Jessy, and it didn’t fix my problem, unfortunatly. In fact I had hard time with the rolloff reverting back to 0 right in the editor. The simplicity of use is really neat tho, auto detection of proper game objects and components is nice, I have alot to learn from this script… : )

I did try in my own script to manually calculate the rolloff with the root of the distance (kinda tried to do the same as jessy script, but right were I calculate the sound volume), and still it didn’t work.

I’m faily new to Unity, so it’s very probable I did some funny thing in my game that might be the problem. I have a background in programming, but never really coded for games and it’s been awhile since last time I coded something. Didn’t had much time to create that game too. Hearing from the community tho seems Unity have some problem with audio, switching from OpenAl to FMOD in 2.6, and stuff.

Yesterday I tried to do a very simple application with one listener attached to the camera, and an audiosource with the same audioclip I used in the problematic game, without significant results. It seems it really have something with my game, so I did a stripped down version and sent a problem report…

Thanks for the help, guys!

hope this helps!