How to Play Sounds on Collision in Unity
Maximizing your sound capabilities through FMOD
Unity sound engine offers decent performance for most projects, but it falls short when looking for more advanced audio features. With this project, we aim to mix the power of FMOD Studio with Unity’s physics engine.
The shortcomings of Unity’s audio engine
This is a project I made some years ago with a colleague of mine. The idea was to make a tool to synchronize Unity’s physics with FMOD, allowing users to take full advantage of the latter’s audio capabilities.
You can find the source code here. Next is a short tutorial I wrote at that time to explain why and how you should use this tool.
In FMOD Studio, the base unit is the event, if you want to play a sound, you must create an event and assign audio file(s) to it. This means you could change the entire audio of a game while keeping the same event structure.
For example, if you are making a shooter, at some point you’d probably like
your gun to play a shot sound. If using Unity, you’d need to add an Audio Source component to your gun, and then make it play from your code when shooting. That’s okay, it’s easy to understand and pretty straightforward.
But if you try to make it more complex, you start to notice some weaknesses. If you tried to make slightly different sounds for each bullet to avoid repetitiveness, you would need to code it. But how the game sounds is a problem that should be taken care of by the sound designer, so a programmer shouldn’t spend time on that.
If you were using FMOD Studio, the sound designer could just randomize a parameter every time an event gets played, and apply different effects depending on that. As easy as that.
While the FMOD Unity integration can be easily handled, synchronizing and tuning it up correctly with the physics engine can require much more work than one may initially think.
You can use the trigger function that comes with the FMOD Studio
Event Emitter, but that won’t get you very far, since you’d still need to code
the selection of the event depending on what the GameObject collides with.
That’s where this project comes into play, the idea is to extend the Unity FMOD integration for those who need to play sounds on collision.
How does it work
Let’s take a look at how to set up a simple Collision Sound project. I will just cover the basics, if you want a more in-depth guide you can find it here.
We’ve tried to keep the project as clear and straightforward as possible, but
if you’ve never worked with FMOD before, you should go learn the basics before trying to use it.
It works as follows: from the FMOD side, there’s a project with all the events that will play on collision, it will handle everything related to the sound, while Unity will take care of the logic. You only have to configure the SoundCollider script on the GameObjects you want to behave as such.
I recommend you download this demo, it will be much easier to understand than just reading about it.
In the FMOD Project, there’s a parent folder named SoundMaterials at the root, and each of its events must be assigned to a bank with that
Every subfolder under SoundMaterials represents a different material, and
it must have at least one event with the same name as the folder. This is the
default event, it will be played when colliding if no other more specific event
can be found.
Specific collision interactions can be created by adding one event with the name of one of the materials under the folder of the other material. e.g. “wood/metal”: metal event under the wood folder, it will be played every time wood collides with metal (or vice versa).
Unity setup is very simple, just import the package, add an FMOD Studio Listener to your camera, and start adding SoundCollider components to anything you want to play sounds on collision. Just remember that it must have a valid material name and as well as a Collider [3D/2D] and a RigidBody [3D/2D] (unless the collider is a trigger).
Those are some of the key features of Collision Sound:
- Generic & specific sound material interactions (different events depending on what collides with what).
- Built-in parameters that auto-sync in runtime: velocity, mass, and size.
- Custom parameters, you can set your custom FMOD parameters from code for any SoundCollider.
We’ve put up a very simple demo in Unity to show how this project works and how it should be used.
Thank you for reading to the very end, I hope you liked this little project. If you didn’t, please let us know why and how would you improve it, it would be of great help!