- Home /
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.
Answer by Soby · Feb 12, 2011 at 01:48 AM
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. :)
Answer by Soby · Feb 14, 2011 at 02:09 AM
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:
you will need to reformat the script... didn't c/p the way i had it originally.
Answer by kennypu · Feb 12, 2011 at 12:26 AM
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 $$anonymous$$es 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
well you can raycast from the player(or sound source) to the enemy, and if there is a wall in between, you can reduce the noiseLevel variable, thus weakening the sound.
Answer by DaveA · Feb 12, 2011 at 01:13 AM
As kennypu said, but you could also cast a ray between the player and the sound and 'attenuate' based on what it hits in between. Not exact, but could be close enough.
Answer by celadon · Feb 12, 2011 at 01:19 AM
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).
Your answer
Follow this Question
Related Questions
Setting Scroll View Width GUILayout 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Finding Positions of other game objects and using them as an argument for an if statement. 0 Answers
AI wandering script needs help. 0 Answers