NPCs responding to sound cues

Does anyone have a method for triggering actions in NPCs in response to non-scripted audio cues?

For example:

  • Guards are asleep but wake up if there is a loud sound.

  • An explosion or crash causes NPCs to act terrified and run away from the source of the sound.

Ideally you could have multiple audio listeners attached to NPCs in a scene (just as you have multiple cameras) and could read the overall volume present in each listener - if above e certain threshold you could activate the desired behaviour in the NPC. Of course, Unity only lets you have one audio listener.

So I’m wondering what is the best way to simulate hearing in NPCs - so they can respond to audio cues, such as explosions or similar?

I wouldn’t make them react to the noise persay, but give the "impression"they are reacting to the noise. So no real noise is “heard” by the NPC, instead it is just a collision trigger box for the area of the noise and based off it’s collision with the NPC makes them “hear” the noise.

That would be how I would approach it anyway

I working on the same problem and I’ve found only one way to solve it - use a sphere collider to notify NPCs about sounds around. All you need ia add one MonoBehavoir component that will catch OnTriggerEnter event and process it.

If you will find another one solution for this issue it would be great if you share it here)

Hmmmm I think the collision method is how most games deal with it as it is an easy and less resource intensive method

I’ve been thinking along those lines as well. I’d like something that works both with smaller sounds close up as well as big sounds far away - Imagine the following situations:

You’re in a room with an NPC and knock a cabinet over - the NPC jumps in fright.

You’re in the open and a plane crashes - the NPC looks around startled and reacts with fear.

If multiple audio listeners were an option this would not be a problem. Maybe I should request it as a feature?

I suppose I could check

AudioListener.GetOutputData(int,int)

Although the NPCs would be reacting to what the player hears (rather than audio from their own position) it might work well enough to convince.

I am unsure why you need them to actually hear the noise when you can just work around a more simple and less costly method that gives exactly the same result with much simpler methods. Stop trying to re-invent the wheel :stuck_out_tongue:

Hmmm, “trying to reinvent the wheel” means a thing has been done already - but there is no existing way in Unity to actually check the level of noise present at various 3D positions.

I know there are methods using triggers etc. to achieve the same effect, but they are just that - workarounds :slight_smile:

Btw, Playstation 4 incorporates ray casting for audio. I would be surprised if we don’t see this as a feature in Unity before long.

http://www.google.com/patents/US8139780