Alvin Wiltan
Alvin Wiltan

Reputation: 61

unity3d player gameobject "hear" sounds

I have a question that is a little... complex

basically I want a gameObject (an enemy) "listen" to the sound of the player (footsteps, door opening, gunfire etc)

I could do this just fine by using:

>Collider[] hitColliders;

>hitColliders = Physics.OverlapSphere(transform.position, 10f,enemyLayer);
>
>for(int i=0;i<hitcolliders.Length;i++){}// Call Enemy Hear Player Function

However, the problem starts here:

I want the "sound" to be blocked by walls. At first, I thought I could use Physics.Linecast to do this. However, this means the sound could be blocked by a mere piece of pole, because Physics.Linecast only shoots a single, straight line.

Example Picture

What I want is player C could not hear anyone at all, but player A and B could still hear each other.

I hope you guys understand my question.

Upvotes: 1

Views: 467

Answers (2)

philont
philont

Reputation: 130

Leave all this Physic and other graphical stuff behind.

Create a separate logic for this behavior.

Create a big chess board (where x,y will be x,y position in you game) and add only sound-generators, sound-listeners and sound-obstacles.

Update this chess board each frame and create sounds, fly sounds, block sounds and listen to sounds and act appropriate as the real life actors.

This way you can add parameters to each obstacle, sound-generator and sound-listener so that all be configurable.

Upvotes: 1

Marius Junak
Marius Junak

Reputation: 1213

You could place the objects you want to block the soundwave on a specific layer. And just make the raycast ignore all other layers.

Upvotes: 1

Related Questions