Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Multiple enemies with their own audio sources and clips makes my game's audio lag.

Discussion in 'Audio & Video' started by Shibilicious, Aug 10, 2015.

  1. Shibilicious

    Shibilicious

    Joined:
    Feb 18, 2014
    Posts:
    40
    Hello!

    I'm making a game that will have tons of enemies in one scene and each of them has their own Audio Source and Clips.

    My issue is, when they all start to stack around me and shoots me, the audio lags so much. The background music is cutting off, my shooting sfx is cutting off, explosion sfx is cutting off, etc...

    Basically what's going on is that there's like 50+ audios playing at the same time and the sound breaks :/.

    I'm using the PlayOneShot function on everything, btw.

    What's the best way to handle game audio in terms of having multiple audio sources and clips at the same time?
     
  2. michaelhartung

    michaelhartung

    Joined:
    Dec 19, 2013
    Posts:
    72
    What file format are your sfx? If they're ogg or mp3 there will be a lot of decompression going which is CUP intensive. You could also check your max voice count in the audio settings + the priority settings of the individual Audio Sources.
     
  3. Shibilicious

    Shibilicious

    Joined:
    Feb 18, 2014
    Posts:
    40
    I'm using WAV format.

    I went and checked my Audio Settings and I have 1024 on virtual voice count and 128 on real voice count.

    What does that mean? (I'm new to audio stuffs)

    I've also thought about the priority settings, yeah.

    I only have 2 sound effects at the moment and that's shooting sfx and explosion sfx. Shooting is at #2 priority and Explosion it at #1 priority. But again, the issue is when 30 enemies are shooting at the same time, all the shooting sfx stay at the same priority.
     
  4. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,555
    You don't want to have 30 of them shooting at the same time, that can be super loud and performance-draining. You'll need to make centralized code to handle that.

    That's the initial reason we created Master Audio, before it evolved into a more fully-featured audio plugin.
     
  5. Shibilicious

    Shibilicious

    Joined:
    Feb 18, 2014
    Posts:
    40
    Yeah... I figured that it's a very bad game-development practice...

    Master Audio is something that I can't afford at the moment.

    Is there anywhere I could read or learn about how to do the "You'll need to make centralized code to handle that." or a general guide on coding efficiently with audio?
     
    Last edited: Aug 10, 2015
  6. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,555
    Basically we don't put Audio Sources "on" any game objects like players, enemies, weapons, etc. Instead all Audio Sources are all located under a central MasterAudio game object by name of audio clip. Then there is code keeping track of how many of each are playing (so you can limit them to say 5) and it moves the Transform of that sub game object to the location of the object that's supposed to be making the sound. By keeping the audio on a separate game object you also defeat the problem of making a death sound that gets cut off when the enemy gets destroyed.

    If keeping track of how many is too complicated (it can be), instead just look at all the child game objects and see how many are playing each time.

    As far as reading about how to do it, nowhere that I know of. I just figured out stuff as we went along and run into limitation after limitation.
     
    Westland likes this.
  7. Shibilicious

    Shibilicious

    Joined:
    Feb 18, 2014
    Posts:
    40
    I think your explanation is more than enough for me to work around it.

    Thank you so much!
     
    jerotas likes this.
  8. AyeJayCee

    AyeJayCee

    Joined:
    Dec 23, 2012
    Posts:
    3
    You are a real hero, and a real human bean.
    Gonna check out master audio in future projects that have budgets it sounds like an optimum solution :)
     
  9. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,555
    Thank you sir.
     
  10. kristiangrundstrom

    kristiangrundstrom

    Joined:
    Nov 27, 2017
    Posts:
    38
    I'm not a programmer so not sure how one go about this but as already said a drastic limit in audio sources is a good start. Our brains can't percieve too many real-life inputs so a limit of just a couple of sources would still serve its purpose.

    Next time you're in a crowded place - like in a mall or busy street. How many distinct conversations or even footsteps can you make out? We don't register much individual sounds as much as the collective sounds in a setting so just a few sources would be fine. :)

    What about 2-3 weapon sources and an extra layer of more intense music?

    Hope you get the issue sorted!