Advice on AI reacting with sound

Hey there.

So I’m going about learning AI in ue4 (working only with blueprints). I have read the documentation on things like blackboards, blueprint trees, decorators, services etc and I have also watched (and followed along with) the AI related twitch streams so I have a reasonable understanding of the most basic principles of AI.

So what I’m trying to do is have an AI react to a specific sound…I have a first person character with basic movement etc and a basic rifle that fires a projectile and plays a gunshot sound when fired. What I’m aiming to do is have an AI controlled pawn complete an action whenever this sound is first played, so that the first time you shoot the rifle, the AI will either play a duck animation or run away from where the sound came from.

I have tried hashing my way to a result with a few attempts with different approaches, but I haven’t been able to achieve the result I’m aiming for (or anything close to it).

So if anybody could give me some advice on how to approach this that would help me a great deal as atm I’m not sure whether I should be using pawn sensing, ai perception, how I should be using blackboard and behavior tree etc to do this in a way that is clean and sensible.

Cheers. :slight_smile:

It’s a matter of preference. Me personally would flip a bool whenever you shoot and the ai is close enough to hear it. This bool is in the blackboard and will change a state in the behaviourtree to duck or run away. A second bool is needed if you only want it to be for the first shot.

I wouldn’t use the perception system with the reason that I feel like it would be overkill for this scenario. However if you think in the long run and need specific interaction to different sounds (maybe explosion, buildings collapsing, player playing certain music,…) it might be easier to use perception as it (seems) easier to “sort” behaviour for each “sound”.

NOTE: Haven’t used ai sound perception ever so I might be wrong. Just going with what I read from documentation.

Try to learn to use trigers in volume menu. You can just make spherical triger and attack it to your character. And set minim radius by 50 and use this to simulate noise propagation.
And in the AI make different allert states controlled by a variable
So if AI was in range for the trigger volume to periodically increase the allert until it fill.
You can controll volume scale to increase our decrease volume triger in function of how big was the noise sound. And after you increased it to periodically decrease using event tick node until is back to normal. In this way you can make for character tallents like face to face fight our silenced fight by tweeking the triger scale.

Cheers, this is sort of how I thought I might go about it, Ill see if i can figure out how to set it up right and let you know how it goes.

Thanks

Hey, I never even considered using triggers, sounds quite interesting, ill give this method a go if I can’t sort it out the way i’m trying atm. Thanks for the advice.

It’s usually better not to use physics if possible

I tried to set it up so that the gunshot makes a noise that can be heard by the pawn sensing from my AI_Controller which triggers a bool value to be set to true which is then setting a condition for the sequence node for when the value is set to true and then forcing the AI to move to a new location, however the AI is moving there before I shoot when the bool should be set (by default) to false…and i’m not quite sure why this is happening. Below are my AI_Controller blueprint, Behaviour Tree, and Player Character blueprints.

Any pointers on where I went wrong would be great. Thanks for the help already!

Is this because it would make processing more intensive / stressful?

Hey, great job setting this up! A few pointers:

  1. There should be no need to call run behaviourtree after setting bool in tick. Just calling in beginplay is enough.
  2. In behaviourtree you use conditional loop. If i understand documentation correcty it means that this part will loop unless the condition is met. I think what you need is decorator->blackboard. (This part is probably the most important)

Hope it helps!

Thank you so much for the responses. I adjusted both of those points and now its working perfectly…I think I need to reread through all the documentation again because I definitely mistook the purpose of the conditional loop decorator, I think I just need to read through a bit more carefully :>.

Seriously though this was very helpful, it benefit my understanding greatly.
Thanks:)