Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sound gets distorted when played

Hi folks,


in Unity3D when I try to play sounds of multiple explosions I get weird explosion-sounds, i.e. they sound distorted. I already tried to set the dopplerLevel to zero but that didn't do the trick. I do not get any bugs. Any help, please?😪


//sets up an array of AudioClips and gets populated via Resources.Load

AudioClip[] enemyExplSnds = new AudioClip[7];

for (int i = 0; i < enemyExplSnds.Length; i++) {

      enemyExplSnds[i] = Resources.Load("Sound/explosions/enemies/explosion_" + i) as AudioClip;

}


//this plays random sounds

int randomExplSndVal = Random.Range(0, enemyExplSnds.Length);

SoundManager.PlaySFX(enemyExplSnds[randomExplSndVal]);


//This is the method that plays the sound:

public static void PlaySFX(AudioClip sfxClip) {

//PlaySFX is the simplest, all we do here is get the SoundManager instance, fetch a new SFXSource and use it to play

//the clip before stating the RemoveSFXSource coroutine to clean it up.

SoundManager soundMan = GetInstance();

AudioSource source = soundMan.GetSFXSource();

source.volume = GetSFXVolume ();

source.clip = sfxClip;

source.loop = false;

source.Play();

soundMan.StartCoroutine(soundMan.RemoveSFXSource(source));

}


//This is the method GetSFXSource:

AudioSource GetSFXSource() {

// set up a new sfx sound source for each new sfx clip

AudioSource sfxSource = gameObject.AddComponent<AudioSource>();

sfxSource.loop = false;

sfxSource.playOnAwake = false;

sfxSource.volume = GetSFXVolume();


if (sfxSources == null) {

sfxSources = new List<AudioSource>();

}


sfxSources.Add(sfxSource);

return sfxSource;

}


//This removes the sound:

IEnumerator RemoveSFXSource(AudioSource sfxSource) {

yield return new WaitForSeconds(sfxSource.clip.length);

sfxSources.Remove(sfxSource);

Destroy(sfxSource);

}

Sign In or Register to comment.