A gameobject trigger that plays a sound on another gameobject.

Hi,

iam doing a horror game and needs help with something…

i need a sound to play that comes from another room, that the one the players is in,

So when the player hits a trigger game object in the room his in, a sound shall play in another room where another gameobject is with an audio source…

Can anyone help me with a script…

Sorry my bad english.

Thanks in advance…

Create an object with an AudioSource attached and place it in the room you want the noise to come from, then create an object with a collider with isTrigger set and attach a script similar to the following:

public class NoiseTrigger : MonoBehaviour {
    public AudioSource source;

    public void OnTriggerEnter(Collider other) {
        if (other.gameObject.GetComponent<Player>() != null) {
            source.Play();
        }
    }
}

Then drag the object with the AudioSource attached onto the NoiseTrigger component.

The check for whether the object is the player will depend on how you do that in your game. It could be a check against a LayerMask or a tag, etc. You might also want to disable/destroy the NoiseTrigger component after triggering the sound, so the noise isn’t repeated every time the player hits the trigger.