Search Unity

Question Sound gets distorted when played

Discussion in 'Audio & Video' started by drpelz, Jan 29, 2022.

  1. drpelz

    drpelz

    Joined:
    Dec 7, 2017
    Posts:
    69
    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?


    Code (CSharp):
    1.  
    2. //sets up an array of AudioClips and gets populated via Resources.Load
    3. AudioClip[] enemyExplSnds = new AudioClip[7];
    4.         for (int i = 0; i < enemyExplSnds.Length; i++) {
    5.             enemyExplSnds[i] = Resources.Load("Sound/explosions/enemies/explosion_" + i) as AudioClip;
    6.         }
    7.  
    Code (CSharp):
    1.  
    2. //this plays random sounds
    3. int randomExplSndVal = Random.Range(0, enemyExplSnds.Length);
    4.             SoundManager.PlaySFX(enemyExplSnds[randomExplSndVal]);
    5.  
    Code (CSharp):
    1.  
    2. //This is the method that plays the sound:
    3. public static void PlaySFX(AudioClip sfxClip) {
    4. //PlaySFX is the simplest, all we do here is get the SoundManager instance, fetch a new SFXSource and use it to play
    5. //the clip before stating the RemoveSFXSource coroutine to clean it up.
    6.         SoundManager soundMan = GetInstance();
    7.         AudioSource source = soundMan.GetSFXSource();
    8.         source.volume = GetSFXVolume ();
    9.         source.clip = sfxClip;
    10.         source.loop = false;
    11.         source.Play();
    12.         soundMan.StartCoroutine(soundMan.RemoveSFXSource(source));
    13.     }
    14.  
    Code (CSharp):
    1.  
    2. //This is the method GetSFXSource:
    3. AudioSource GetSFXSource() {
    4.         // set up a new sfx sound source for each new sfx clip
    5.         AudioSource sfxSource = gameObject.AddComponent<AudioSource>();
    6.         sfxSource.loop = false;
    7.         sfxSource.playOnAwake = false;
    8.         sfxSource.volume = GetSFXVolume();
    9.  
    10.         if (sfxSources == null) {
    11.             sfxSources = new List<AudioSource>();
    12.         }
    13.  
    14.         sfxSources.Add(sfxSource);
    15.         return sfxSource;
    16.     }
    17.  
    Code (CSharp):
    1.  
    2. //This removes the sound:
    3. IEnumerator RemoveSFXSource(AudioSource sfxSource) {
    4.         yield return new WaitForSeconds(sfxSource.clip.length);
    5.         sfxSources.Remove(sfxSource);
    6.         Destroy(sfxSource);
    7.     }
    8.