Language

Enemy sound detection

OK, In my game that is a FPS style game, Im trying to do a sound detection, and for this I have many ideas but I actually don't know how to make that in a script so first I want like a combination of sounds that it actually depends on the volume of the sound, the distance between the enemy and the sound and stop where is a obstacle(something like this). And for last I want something like detection with reverb zone so like that it will depend on how far does the sound was to the enemy.

Note: I am using JavaScript and if the answer is a code it would be better :).

Please if someone knows something about this please answer and if you have questions of this question please comment.

5 Replies

This is actually kinda funny to see posted cuz im going to be working on a solution to this tomarrow.. their's 2 ways Im going to be lookin into.. which ever one works first I'll use or if none work I'll go back to the drawing board... Though the raycasting thing seems like one of the easier ways but Im curious because racast is only in one direction no? so if your not facing the enemy how will that help? or are you talking about doing a spherecast?

the 2 ideas Im going to be trying are:

there's suposedly seperate listeners for this type of situation that can be placed on enemies and depending on how far away you are you can set up the roll off and such to get the distances you want for multiple detections... for me we need it for stealthing around and making sure the enemy can still see you if your right infront of them but muffle the noise in stealth mode so they can only really hear you if your runing, walking, jumping...

My idea is not fully looked into but I seen somewhere you can actually access a calculated distance between objects, from what I gathered it didnt mention a raycast of any sort but I would imagine it would need one. But simular to the above, just use a variable named distance which is Distance and its suposed to calculate distances to what ever you assign it to. Again this isnt fully looked into so there's alot of gaps in it atm.

if I do happen to get it to work tho, I'll be sure to post it here... I too am using JS.

From the manual: Each scene can only have one Audio Listener. The Audio Listener is for the player (the user interface), not for the objects in your game world.

You can use Vector3.Distance(position1, position2) to get the distance between two objects

I think your steps would be: a. use Physics.CheckSphere (not a spherecast) to find all of the colliders in a given radius around the character b. identify the ones you care about (e.g. enemy) and use the Vector3.Distance to find out how close they are c. then do the raycast between those two objects to check for a wall or whatever.

Thats what I originally thought with the audio listener bc thats what i read as well. However someone else mentioned there is a different type of listener for this kind of situation. Im not entirely sure if its possible or true but I plan on lookin into it since it came from my lead. However the way you stated is the way i was reffering to and it was my first idea. Which thus far my 1st idea usually work out the best. :)

Put this on the enemy but you will need to alter it based on your needs. This is set up for seathling and such... this doesnt take into consideration of walls but you can do a simple raycast or simplly dont put them close to a wall lol

the formatting on here got all wacky so i posted it on one of my blogs the link is below:

instead of actually trying to detect a "sound", you can just set a variable that stores like a noiseLevel, and if the distance from the player to the enemy is smaller than the noiseLevel, the enemy knows you're there.

for example, when you're not doing anything, the noise level is 0. when you're walking it is 1. if you shoot, it is 10. and if the distance from the enemy to you is smaller than the noiselevel, they find you.

yeah that sounds a good idea, but I also considering to make an stop sound, that mines that if there is a wall between enemy and sound there would be not detection or less detection that if there is not a wall, but thanks any way, I never got an idea like that:P

I also briefly wondered about this, but quickly rejected it as totally infeasible - it would need to be modeled by the physics engine, and you would not only need to model all of the audio sources, and the audio listener, but every material would have to be given values for how reflective and/or absorptive it is... way too much computation.

You could always do a raycast between the sound source (e.g. character) and the "listener" (e.g. enemy) and try and detect any sound barriers between them. If found, you could adjust the sound level/distance calculation. (You would have to decide how to detect the sound barriers and what kind of adjustment to make).