- Home /
Sound Trigger
Hello i'm Max,
i was wondering how to trigger a sound. eg. the player walks through a door as he enters the room a sound plays then stop 2 second after.
Answer by aldonaletto · Aug 07, 2012 at 01:37 PM
This is easy: create a trigger volume and adjust its dimensions to cover the desired area (menu GameObject/Create Empty, menu Component/Physics/Box Collider, check Is Trigger), then add an AudioSource to it (menu Component/Audio/Audio Source), assign the desired sound to its Clip property, and attach a script like this to the trigger volume:
function OnTriggerEnter(other: Collider){ if (!audio.isPlaying){ audio.Play(); } }This will play the sound once. If the sound must be repeated for 2 seconds, convert it to a coroutine (just use yield inside it):
function OnTriggerEnter(other: Collider){ var t: float = 2; while (t > 0){ t -= Time.deltaTime; if (!audio.isPlaying){ audio.Play(); } yield; // let Unity free till next frame } }
Answer by Meltdown · Aug 07, 2012 at 01:19 PM
Use a combination OnTriggerEnter and AudioSource.Play to do what you need.
ok I have a camera that is animated, i did exactly what you sad, but it did not work??? what have i done wrong??
Your answer
Follow this Question
Related Questions
I want my trigger to play sound only once! 2 Answers
Triggering random sound on player 1 Answer
Totally new noob to mecanim and unity itself - Trigger animation? 0 Answers
On trigger , sound play 2 Answers
Activate sound without Pro filters 0 Answers