This assumes that you're not worried about audio occlusion (sound muffling around obstacles like walls etc.).
I would have a circular/spherical trigger collider (SoundCollider) attached to your gun/weapon that is the size / radius of how far you want the sound to travel. When you shoot: it that turns on this SoundCollider. Enemies can have an appropriate Rigidbody to collide to attached to them (Listener) to collide with the trigger and a handler in a component that getshandles what to do if they are triggered if they are in range of your SoundCollider.
Couple of Pros I can see to this approach:
- You can have an easy to edit variable that changes the radius of the
SoundCollider, making some weapons quieter than others. - You can let Unity handle the events between your Gun sound and the enemies in hearing range.
- Effects can be applied to the collider if necessary so when it is active a radar on the mini-map (if you need one) can show the area that sound will travel to.
- As you only need to turn the
SoundCollideron or off instead of sending multiple events, it will work regardless of firerate on your weapons as you simply turn it on when you start firing and have a small delay to turn it off once you've stopped. - You only need
Listeners on appropriate enemies and you don't need to use Tags to filter through them. Disabling aListenercan make a unit "deaf" quite easily if you need them to not hear anything under certain conditions (interactive cut-scene).
It's a concept but can work well, I used something similar to make an AOE damage ability on a unit but the concept is essentially the same - Unit in certain proximity triggers code when ability is active (gun is firing).