- Home /
Problem with triggering repeated audio on entry and ending it on exit from trigger
I am trying to set up a script that will trigger an audio clip when the First Person Controller Player enters a trigger zone described by a mesh collider. The mesh collider is set with Is Trigger checked true. The FPC is operating normally, and the audio clip is imported into my assets with no problem. I have been able to follow some C# and javascript examples to get the audio to trigger on entry to the trigger area, but I haven't been able to get it to loop or repeat continuously exactly until the FPC Player exits the collider.
My problem seems to be closest to this one: http://answers.unity3d.com/questions/408115/play-audio-sources-in-trigger-zone-when-player-ent.html and I think I should be using OnTriggerEnter and OnTriggerExit. However, I haven't been able to figure out exactly how to trigger audio on entry, loop/repeat until exit, and turn it off on exit. I don't have multiple audio sources though, just a buzzer sound that should play continuously whenever the Player is located within a wedge-shaped mesh.
The other scripts in my project are in C# so it would be great if someone could help me with a C# script, but any help would be appreciated!
Answer by Nerevar · Jun 06, 2014 at 10:23 AM
Hi
You just need to set the parameter loop of your audioSource to true in the inspector to make you clip repeat.
or audio.loop = true; somewhere in your script.
And with all the tutorials (I suggest this one, it is in .js but the principle is the same) about OnTriggerEnter and OnTriggerExit you should figure out how to handle your audio with :
audio.Play();
audio.Stop();
Answer by kristen2001 · Jun 07, 2014 at 12:05 AM
Thanks for the link to the tutorial, it answered my question completely (and even made and then corrected some of the same errors I made earlier).
Your answer
Follow this Question
Related Questions
Using triggers to repeat basic scenes 1 Answer
Alternatives for a looping title scene 1 Answer
?Can you loop a terrain, to make it never-ending? 3 Answers
Looping an AudioClip var... 1 Answer