General question regarding collision sounds

After following the excellent tutorial about sound effects & scripting, I have a good understanding of how to add collision sounds to GameObjects. However, in a “real-world” application, things are usually more complex:

For example, the sound that a baseball makes when hitting a beer can is very different from the sound it makes when hitting a wooden wall, or a grassy ground. At the same time, the sound the beer can makes when it hits another falling beer can, or a wooden table, or again the ground is also different (not to mention the differences between the can being hit laterally or at the top/bottom).

In the end, the required collision sounds would not be a simple “play this audio clip when a collision with anything is detected”, but rather a 2D matrix of “play this sound when an object of type X hits an object of type Y”. This leads to the question where we would implement the logic for that matrix? Adding it to each object sounds like a maintenance nightmare (basically a huge switch-statement selecting the sound depending on the colliding object type), and also be redundant (for example, object “Baseball” would play a sound when hitting “BeerCan”, but object “BeerCan” would also play the same sound when colliding with an object of type “Baseball”.

Another topic is how to efficiently identify the “type” of the colliding object? One (easy, but dirty) way I can think of is using tags. Another one would be to dig into the Collider object of the other object, and determine its material.

So, what I’m asking is: Are there any common ways / good practices / recommended approaches to deal with this topic? I’m thankful for any ideas or pointers to relevant documentation or tutorials.

Yes, we use tags and / or layer specific Inspectors to play different sounds with Master Audio. That’s a good way to go.

I’ve done lots of sound design related programming. I don’t think I’d use a matrix, indeed for the reason that its size has to increase exponentially for each new type of substance. My approach would be based on substances and surface hardness. Each substance would have a single hardness value, and two or three sets of collision sounds depending on the hardness of the other object. Each set would contain between 8 and 16 different sounds depending on the strength of the impact, since altering the volume doesn’t sound natural. So, for every collision, for both objects, resolve based on the hardness of the other object which set to play the sound from, and use the masses and relative collision velocity to pick the “strength” of the sounds. Basically, two sounds will be played for every collision. Finally, you might want to set the pitch on a per-object basis so it correlates to the size of the object. Lower pitches for large objects, higher for small.

Tags doesn’t sound like a good idea, and you’ll probably need a script on all objects that makes handles the collision sounds anyway, making that script the ideal place to define what substance it’s made of. Maybe make the substance a ScriptableObject, in order to make it easily reusable? On the other hand, depending on how comfortable you are setting all this up yourself, Master Audio could be a great and much easier alternative.

2 Likes