- Home /
Play Audio OnTriggerEnter then loop
I was wondering if anyone knew how to start playing audio, then loop it. What I have so far is just a PlayOneShot, but I'm not familiar with how I would go about writing a script that checks to see if the audio has played in it's entirety, then play the audio again and again in a loop.
Here's the PlayOneShot script, using a Trigger to play the audio.
var Sound : AudioClip;
private var hasPlayed = false;
function OnTriggerEnter(){
if(!hasPlayed){
audio.PlayOneShot(Sound);
hasPlayed = true;
}
}
Does anyone know of a way to play the audio in a loop after the whole thing has played? Using the "Loop" check box on the audio source won't work obviously, since PlayOneShot cancels out that function.
Answer by ExpiredIndexCard · Apr 25, 2013 at 11:10 PM
Don't use PlayOneShot. Try using
//put in update function and also take out the playoneshot
if (!audio.isPlaying && hasPlayed)
audio.PlayOneShot (sound);
Hey ExpiredIndexCard, I tried this method, and it seems to work, as in it plays the audio and loops it, but for some reason it seems to be playing every frame, so I get a bunch of layers of the same sound playing on top of eachother. Any idea why this might be? The script looks like it would avoid this problem.
Answer by Johnnemann · Apr 26, 2013 at 12:26 AM
You could use the loop checkbox on the AudioSource, but uncheck "Play on start". Then, in your OnTriggerEnter, just do AudioSource.Play(); In OnTriggerExit(), do AudioSource.Stop(); (assuming you want the sound to stop when the trigger is exited).
Your answer
Follow this Question
Related Questions
Audio loop after time 2 Answers
How to start/stop/loop different music tracks 0 Answers
Stuck in Teleporter Loop 2 Answers
Audio fade after entering trigger? 0 Answers