hi everyone I’m looking for some information on how to make sounds smoothly transition from one to another, is their any useful tutorials or anything at all that i can have a look at.
basically if i had one single audio file, say of an engine running. and then i cut it in half and reimported the two separate files. how would i smoothly play from one to the other without any clicks or pops, from a touch of a button.
i looked at the unity car demo, but the sounds dont work on it anymore, and when i did get them to work i realised that its using gear changes to make the switch between audioclips,
You could cross-fade the two sounds, but this would require two AudioSources attached to the same object: reduce the volume of one audio to zero while increasing the volume of the other during some time - 0.5 seconds, for instance. In order to get references to the AudioSources, you may add them at Start and save their references in variables - that’s how the car demo handles the several AudioSources attached to the car.
var sound1: AudioClip;
var sound2: AudioClip;
private var audio1: AudioSource;
private var audio2: AudioSource;
function Start(){
audio1 = gameObject.AddComponent(AudioSource);
audio1.clip = sound1;
audio2 = gameObject.AddComponent(AudioSource);
audio2.clip = sound2;
}
function CrossFade(a1: AudioSource, a2: AudioSource, duration: float){
var v0: float = a1.volume; // keep the original volume
a2.Play(); // make sure a2 is playing
var t: float = 0;
while (t < 1){
t = Mathf.Clamp01(t + Time.deltaTime / duration);
a1.volume = (1 - t) * v0;
a2.volume = t * v0;
yield;
}
}
...
// crossfade sound2 to sound1 in 0.6 seconds
CrossFade(audio2, audio1, 0.6);
Hi there, I’ve been looking for a solution to this and this code looks good. I’m just not sure what’s meant by “you may add them at Start and save their references in variables”. Can someone elaborate please?
Thanks in advance.