I am making an RTS, fights can be between hundreds if not thousands of units. It seems unreasonable to have individual audio sources on all these units to play the swoosh sound of the sword or the grunt when getting hit.
I say fake it. No human being can image thousands of sounds in their head at the same time, so why waste time trying to get that to work? Have a looping, generic battle noise track and maybe 8 to 10 sources go off at random to mix it up a little. Maybe have a few sources on standby for high-priority sounds.
I would also say fake it. We have a audiosource pool in our game. We can request a play at point and when it’s finished playing it will be returned to pool.
Prioritise your sounds and play only the ones which matter most. If you play too many sounds at once it’ll just turn to moosh and noise anyway.
Once you’ve figured out which sounds matter and how to manage playing just those ones, it’ll also be easy to share AudioSources.
For instance, you could have a SoundEffectManager class used to play sound effects. Instead of your units directly playing sounds, they call something like SoundEffectManager.RequestPlaySound(…). The manager can then do something snazzy, such as:
Check if a matching sound has played nearby recently. If it has, ignore this request.
If the request is granted, grab a matching AudioSource from a pool, put it in position and Play() it.
Provide some settings to control what “nearby” and “recently” mean, possibly on a per-sound basis.
It’ll use fewer resources and sound better. It also gives you a starting point to do some other neat stuff, such as support minor pitch shifts so that often repeated sounds don’t always sound identical, or supplying variations of AudioClips for each effect.
Also the soundmixer (It might require the use of a output mixer on the audiosource which you should use anyway and even is a requirement for APIs like Project Acoustics) have built in prio. If you play more sounds than the hardware can handle it will start to drop sounds based on distance attenuation curves.
But having a domain specific prio on top of that is not a bad thing.
Yes, I’d say its unreasonable. Use the equivalent of LoD. Only handle individual sounds within the immediate proximity of the camera. Use composite ‘unit’ sounds as ‘ambient’ events at longer distances, and composite ambient ‘battle’ sounds for longest distance.
Beneficial side effect : Moving the camera closer gives you a nice audio ‘closeup zoom’ effect as individual sounds become faded in.
Here is Zero-k example, of over 50 units shooting lasers at the same time. An this game can handle 32 players with over 1k units.
You can hear multiple shots nearly at the same time, but there isn’t indeed 50 sounds executed.
Impression Is created.
You still can have many sounds however. But more like different varieties, i.e. laser, cannon, rockets, bullets, units commands and responses, other environments sounds and of course … music.
I thought about this a lot too, I just cant think of a smart way of how to decide what ambient sound to play. If I have 4 swordmen fighting 6 archers, I dont want the same ambient sound as when 20 swordmen fight 40 archers. The possible conditions, while not endless, seem like a lot to handle.
But if you have multiple layers for different ‘audio LoD’, then the ambient sound mix will be different.
So 4 vs 6 if you’re close could be individual sounds, properly drive by collisions etc
Or if further away, 1 of 5 ‘small swordsmen unit’ loops and 1 of 5 ‘small archer unit’ loops.
(Plus whatever loops needed for stuff that’s medium distance
Plus whatever loops needed for stuff that’s long distance
Plus generic background battle loop/walla)
20 vs 40 could be 4 of 5 ‘small swordsmen unit’ loops, or 2 of 5 ‘large swordsman unit’ loops etc
Depends how you do it, and how you layer small/medium/large versus ‘LoD’.
Also, to mask repetition
make sure you have multiple loops for each unit
make sure the loops are long enough to mask repetition
make sure the loops for different units are non-matching in length
consider using separate weapon/voice loops in parallel
avoid memorable ‘standout’ sounds especially voice lines
Also; remember a loop doesnt have to be a single baked sample. It could be a sequence of individual samples to play, and variation in pitch/filtering/volume for variation.
the best way I found is to have a Array of Soundsources and a sound manager that will play audioclips thru those audiosources. Once a AudioClip start a bool will monitorize 2 things is the AudioSource isPlaying and if !isPlaying. At same time lets say if we sent to play a AudioClip with AudioSource[0] we send the configuration too and in that data of audio config we also tell to AudioSource to change position where we need it lets say that will play a sound in our right side . In the meantime if another AudioClip will need to play will check that Array if Array[0] isPlaying then will choose one available Array[1] and we sent the audioclip to play with Audio config and set the position of that in our left side maybe if that is the case (transform.position). In this way we make a Array of 20 AudioSource childs if we think that we will have more than 20 audios at once playing increase the Array . Definally setting a Audiosource to each object in a game will endup by having thousands of AudioSources and from my test can create some performance issues.