Jump to content

How to make AI react to sounds?


Recommended Posts

I want to do that my AI bots can hear sounds and eventually react to it. For example you walk near a bot, he does not see you, but he can hear your footsteps and then eventually go to the source of the footsteps until he sees you, or same if he hears a shot.

What would be the best approach? In chat Jeff suggested a good way would be to keep all the AI players in an array list and every time a sound is played it checks if any of the AI players is in range and if so, have an extra function that eventually makes them react to it.

Has anyone done something like this yet?

Link to comment
Share on other sites

LOLJester's idea is solid. Animation triggers are hooks to call code/script at specific times during the animation. As you pointed out it's already set up to handle footprints, sounds, and particles, but there's no reason that your own custom triggers can't call other scripts. Such a function call might resemble iterating through your AI, checking the distance from the bot to the sound source, ray casting, and deciding how your bot will behave after that.

Link to comment
Share on other sites

Not every sound is an animation, thats just the case for footsteps, a gun fire is not an animation, well it can be, but does not have to be. Other sounds are pickup items, throw items like interaction sounds, or if something explodes.

You can probably bind a large part of sounds to animations, but I don't think it is a good solution to always need an animation to trigger a sound which then triggers something else, it should be within the sound itself.

Another problem especially regarding footsteps is, that since the footsteps are animation triggered you can use an exploit to be able to run without causing footsteps and this is through skidding, this works that way, that you press walk and look where the animation plays and then you only slightly press the walk key so that the animation only plays for a little bit every time without ever hitting the animation point.

Well you can avoid this mostly by placing the footstep at the first frame, but this will lead to cause the footstep sound to happen far too often and will cause funny looking skidmarks from the footprint decals created if you just press the forward key a lot and not hold it pressed and of course the footprints will not match the animation where the foot really hits the ground.

It may work for a quick and dirty solution if it only affects some form of sounds like footsteps, but thats not really what I want. Also footsteps happen quite often, like 2 times per second and more and I don't know how good for performance it is to check for every player on every footsotep if any other player is in range and then do something, it should be more like a on/off thing, like causing footsteps = 1 which is set to 0 when the player stops or sneaks or whatever.

Link to comment
Share on other sites

  • 3 months later...

If you want aiPlayer to react to player as in movement? I always have each AIPlayer know the distance from each Player object. If < somedistance then check if the player is making a sound. Like if the player is sneaking past like crouch move then ignore him unless the AiPlayer can see him. I also have a timer where if the player moves too fast like running then assume you can hear him. But if I take a step, stop then some time later take another step the AiPlayer is less likely to detect the movement.

For other objects like a gun reload, again distance from AIPlayer < somedistance then detect him. A vehicle < distance then react.

I find it easier to just check proximity which I constantly keep track of then some action will trigger a detect.

Hope this gives you some ideas....

Link to comment
Share on other sites

That sounds like a more elegant solution to just check for the distance and if the player is making a sound and just assume the AI player heard it, probably best also check for the SFXProfile value that determines how far the sound can be heard.

However I was more interested in practical code examples. For example currently by AI players detect the humans by using a container raycast within their field of view. A noise detection would need to be a 360 degree search. Do I make it into a separate functino or just use one 360 degree search and check if a player is detected is he is visible and hearable?!

Link to comment
Share on other sites

  • 6 months later...

The answer seems relatively simple and at the same time, the nuances shouldn't be ignored. I'm also not entirely sure what features are in the current engine, back in the distant past there was a button to show you all the audio emitters as well as their ranges etc etc. This doesn't seem to work currently, but I'm assuming the data is still there somewhere.

Anyway, simple math is run a container search on an area that represents the AI players listening range, and then see what sound ranges intersect with that. The nuance side of it is where you get more complications, like... how does the AI know what sounds are to be reacted to, and which are normal background and ambient noises, this takes a more complex system of testing involving probably an AIs ability to differentiate, as well as an alert factor value for individual sounds, where you have gunfire as a high alert of 90-100% say, and burb twitters as 0-10%. Pretty sure none of this is currently set in the engine however so that is a custom engine mod.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...