Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved Overlapping Sound FX create strange noise

Discussion in 'Audio & Video' started by RiceC, Sep 12, 2022.

  1. RiceC

    RiceC

    Joined:
    May 29, 2014
    Posts:
    7
    Hello everybody!

    I guess this is a common issue but I can´t find a tutorial to help me solve this problem.

    The thing is that, in my game, at certain points, lots of enemies are destroyed simultaneously, and because of that a lot of destroy Sound FX play at the same time, which makes a strange noise, like the speakers are about to break x)

    The way it works is that each enemy has a Prefab with the SFX attached, and it destroys itself once it has ended playing.

    If you know how to solve this problem, please help me.

    This is the code:

    Code (CSharp):
    1. public class SFX_Instantiation : MonoBehaviour
    2. {
    3.     //SFX
    4.  
    5.     public AudioClip SFX_ToPlay;
    6.  
    7.     //DELAY SFX
    8.  
    9.     public float setDelayTime;
    10.     float delayTimer;
    11.     bool hasPlayed;
    12.    
    13.     private AudioSource source;
    14.  
    15.     int instanceNum;
    16.  
    17.     private void Awake()
    18.     {
    19.         source = GetComponent<AudioSource>();
    20.     }
    21.  
    22.     void Start()
    23.     {
    24.         delayTimer = 0;
    25.         hasPlayed = false;
    26.     }
    27.    
    28.     void Update()
    29.     {
    30.         delayTimer += Time.unscaledDeltaTime;
    31.  
    32.         if (delayTimer >= setDelayTime && !hasPlayed)
    33.         {
    34.             source.PlayOneShot(SFX_ToPlay, volumeScale: 1);
    35.  
    36.             hasPlayed = true;
    37.         }
    38.  
    39.         if (delayTimer >= setDelayTime + 0.1f &&
    40.             source.isPlaying == false)
    41.         {
    42.             Destroy(gameObject);
    43.         }
    44.     }
    45. }
     
  2. RiceC

    RiceC

    Joined:
    May 29, 2014
    Posts:
    7
    I kind of made a work around!:p

    Everytime the sound must play it is a collider that is spawn into the scene and enters trigger with the player.
    The player is the sound emitter once the trigger has entered. A bool is set so the sound can only be played after x time.
    Code (CSharp):
    1.  private void OnTriggerEnter2D(Collider2D collision)
    2.     {      
    3.         if (collision.CompareTag("SFX_EnemyBreak"))
    4.         {
    5.             if (canPlay)
    6.             {
    7.                 source.PlayOneShot(SFX_BallBreak);
    8.                 canPlayTimer = 0;
    9.                 canPlay = false;
    10.             }          
    11.         }
    12.     }