How to make AI react to sounds?

Scripting questions, discussions, etc
7 posts Page 1 of 1
Duion
Posts: 1006
Joined: Sun Feb 08, 2015 1:51 am
 
by Duion » Mon Feb 05, 2018 9:39 pm
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?
LoLJester
Posts: 65
Joined: Thu Aug 13, 2015 5:58 pm
 
by LoLJester » Tue Feb 06, 2018 9:57 pm
Animation triggers. You can assign triggers in the ShapeEditor. When the animation hits that trigger, a call to script is made. There you can decide whether you want that sound to be heard or not. There is a resource on GG's website that expands on this feature.
Duion
Posts: 1006
Joined: Sun Feb 08, 2015 1:51 am
 
by Duion » Tue Feb 06, 2018 10:10 pm
I'm talking about sounds, not animations. Animation triggers are for footstep decals, particles and sounds and it has nothing to do with AI players.
Caleb
Posts: 18
Joined: Tue Feb 10, 2015 5:01 pm
by Caleb » Wed Feb 07, 2018 5:20 pm
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.
Duion
Posts: 1006
Joined: Sun Feb 08, 2015 1:51 am
 
by Duion » Wed Feb 07, 2018 7:49 pm
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.
Hodo33
Posts: 22
Joined: Fri Dec 29, 2017 4:34 pm
by Hodo33 » Thu Jun 07, 2018 5:59 pm
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....
Duion
Posts: 1006
Joined: Sun Feb 08, 2015 1:51 am
 
by Duion » Thu Jun 07, 2018 6:11 pm
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?!
7 posts Page 1 of 1

Who is online

Users browsing this forum: No registered users and 0 guests