Use the Sound Randomizer package to randomize sounds in your game!
I worked hard to make the code very readable and the tooltips extensive, so theoretically you shouldn’t need this guide, but in case here’s how it works and a place to ask questions/request features if you have any. Also, remember the Sound Randomizer - Advanced Examples package includes a couple others for you to take a look at.
So, with Sound Randomizer…
Two versions exist, a static one and the standard one.
Which do I use?
The main difference is the static one will cut itself off when called before its sound has finished playing, while the standard one can overlap as many sounds as your heart desires. Generally the static one has less of a performance impact, so that should probably be your go to if it can function for your needs.
Standard Version/How to Use
The standard one is a script, there’s a script folder template if you prefer to be more organized.
Attach this script to… something. Specify the sounds you want to pick from in the Sounds array.
Use the script with one of the Play…() functions. PlayRandomSoundEvent() will work as an event call from any other script. If you want to use it specifically from a Trigger and have it play at the trigger’s location, use PlayRandomSoundAtTrigger().
From other scripts you can PlayRandomSound() with a location specified to have it play a random sound at the specified location , or you can PlaySpecificSound() to play a specific sound from your array, also at a specified location.
Musical Chairs is an included example of how to use this version of the script.
Pitch and Volume
You can set the pitch and volume to a static number (the Crayta default value for both is 1), or you can randomize them, or you can randomize the values for each sound individually.
To individually randomize the pitch, make sure Randomize Pitch is enabled, then enable Override Pitch Range. The values you put in the Pitch Range Overrides will correspond to the same Sounds in the Sounds array. So if you have 5 sounds in the Sounds array, the first value you put in the pitch range overrides array will affect the first sound in the Sounds array, the fifth value you put in the pitch range overrides array will affect the fifth sound in the Sounds array. You can extrapolate how to affect the others from there If you only specify, say, three pitch ranges in the override, the remaining two will use the default Pitch Range instead.
It’s an identical workflow for specifying volume.
Changing sound ranges and toggling 3D
In the Object Pooling section, the script takes in a Blank Sound Template, which is just a sound entity. If you would like to configure the range the sound plays, whether its 3D or not, then you can either create a new template or edit the existing one.
Adjusting the Object Pool
The rest of the Object Pooling section can remain untouched, but if you want to mess with it on Init() the script will spawn a number of sound entities equal to the Starting Amount. Each sound entity will re-enter the pool to be used based on the Recycle Time. If you have sounds that last longer than the Recycle Time, they will be cut off when recycled, so please adjust the timing accordingly.
The Max Pool Size is the number of pooled entities you want to remain active. You can set this below your starting amount, or very low, if you prefer. Whenever the amount of sound entities spawned by this instance of the script exceeds the max pool size, it will attempt to cull unused sound entities. It will check every Cull Check Interval (in seconds) for unused sound entities. If a pooled sound entity has not been used for a period of time equal to or greater than the Cull Time, then it will be destroyed.
The static version has nearly identical workflow as the standard version, however it plays off a sound entity it is attached to, and will always play at the location it exists at. It must be attached to a sound entity to function. The Spooky Chord Machine is an example of this.
If you’d like to move it to another location, you can just move the sound entity via your own script. If there is desire to have that functionality added to this script, though, let me know and it’s easy to add. If you trigger it multiple times in a row quickly it will cut itself off, as there is only one sound entity happening. This is great for a static item that doesn’t play its sound too frequently, but basically unusable for something like the firing sound of a gun.
Understanding the Examples
Spooky Chord Machine
This uses the Static version of the script. You can interact with it to play a sound. It’s simply hooked up like this:
The Random Sound Player is a sound entity, which is important. The sound entity settings are incidental and aren’t important to the example.
This is how the script is set up. Can you tell what makes this chord machine “spooky”?
When you interact with the piano, it will randomly choose one of the 6 different piano chords to play.
On each of them, it’ll randomly alter the pitch between 0.1 and 3.
On most of them it’ll play at a volume of 1. However, the Chord - Piano - A Minor sound has a volume override that makes it play at a volume of 10. This randomly much louder sound just might spook you!
This example uses the standard version. You can see it’s 6 identical chairs, whose triggers all call the same script to play a random sound at their location, like so:
Each time the function is called - so each time you walk up to a chair - it will play one of the 5 random pump organ chords, at the default volume of 1. It’ll alter their pitch to be randomly somewhere between 0.5 and 2.
Everything in the object pooling section has been left standard. I’m not doing anything clever with it.
Incidentally the way these overlap probably makes a spookier experience than the other example. If you keep tagging the chairs they’ll keep playing more and more chords. It is a really beautiful experience.
Gun - randomized sound
From the advanced examples package.
This is how the sound randomizer is set up:
There’s nothing special here, really, but why would you want a gun that randomly can play a gun fire sound, bulllet impact, or reload sound?
Well that’s where PlaySpecificSound comes in.
You can see in my (hasty and bad) edit of the gun script, when it plays the shot sound, it plays only the first sound from the sound randomizer.
When it should play the reload sound, it plays only the 3rd of the list.
And when it should play the hit effect sound, it plays the 2nd one from the list, at the location where the hit is specified.
With just altering a few lines of code, adding this one script allowed us to add some random variations to all the (editable) sounds of the gun. All three different sounds share the same object pool, so you’re not flooding your game with more entities than you need. This I’d say is one of the most powerful use cases for the script, in that it can be a one script solution to most of your sound effect needs.
The package also includes some variations on a big floor piano. I invite you to look at how it’s hooked up - each octave has its own personal “sound randomizer” that assigns the right audio asset and pitch alterations to keep the piano in tune (well, equal temperament, can’t have just intonation with a piano…).
Please let me know if you have any questions, bug reports, or feature requests!