- Home /
can foe hear us?
I need some way to detect, can enemy character hear our player. I can check distance betwen player and foe, i can cast ray to check obstacle. But i dont know how to solve "corner" problem. Foe can hear me really good if he stays next to the corner, but raycast will say there is obstacle. Is there any way to work with real sound in U3d?
Here is a possibility that is not perfect but may move you in the right direction. Raycast both from the Foe to the Player and from the Player to the Foo. Find the distance between the two hit points. Scale the distance the player can hear by the some factor based on the distance calculated. The larger the obstruction, the shorter the distance the player can hear.
Answer by YoungDeveloper · Dec 15, 2013 at 02:08 AM
The player itself should cast spherical trigger around it, it's size based on is the player running or walking speed/ loudness.
I think this is the rare time "sendmessage" is usable, or you could call a method by comparing tags. But sendmessage might be better because there can be many enemy's.
When player moves and casts invisible sphere around him, you can get all gameobjects in that certain area, then run a tag search loop checking which GO are "Enemy", then call a method or sendmessage to those gameobjects with coordinates where the sound came from, so instead of Foe automatically following the player, it will go where the sound was, which is realistic.
This is how it could look like. For calling a methods, that is located on "Foe", you will need to getComponent and etc, so sendmessage might be a good idea as i said.
https://docs.unity3d.com/Documentation/ScriptReference/GameObject.SendMessage.html
For sphere cast https://docs.unity3d.com/Documentation/ScriptReference/Physics.OverlapSphere.html Basically you will have an array of all colliders inside that sphere, so from here can find anything, is it a foe, etc.
But as sendmassage takes only one parameter, i don't if that is possible, but you could send a class object, which contains those two coordinates.
Here's an interesting video, how it may look. http://youtu.be/Egy40TYFut8?t=55s
God idea, but we still have a problem, it solve "corner" problem but if there a wall? Foe can't hear us.
You could hear someone behind the wall actually, i think more what you mean if the wall is long, because there can also be short wall, which ends near by.
The solution on this, could be calculation the path distance using path finding grid. If the distance is long, don't go, if its short go.