- Home /
 
Loop Sounds when within trigger
Hello.
I am fairly new to scripting within Unity and need help with making a sound loop whilst the player is walking within a trigger event. The script below make the audio play the clip once but for some reason doesn't wish to loop the audio when I try adding in a timer event. Any help would be greatly appreciated. Many thanks
 #pragma strict 
 
 var splashClip:AudioClip;
 
 var issplash = false;
 
 
 function OnTriggerEnter(o:Collider)
 { 
 issplash = false; 
 if(issplash == false) 
 {
 playsplash(); 
 }
 }
 
 function playsplash ()
 { 
 audio.PlayOneShot(splashClip);
 }
 
 
 
              use OnTriggerStay ins$$anonymous$$d of OnTriggerEnter.
Coding conventions, like starting methods with capital letters (like PlaySplash() ) as well as starting new words with capital letters (e.g. isSplash) will make the code easier to read for others as well as yourself. To solve your problem: Set the looping bool on your audio source to true in your OnTriggerEnter method and run PlaySplash, and to false in OnTriggerExit.
Answer by MaximeS · Sep 29, 2014 at 11:21 PM
I would do something like this ( not tested )
 function OnTriggerEnter(o:Collider)    
 { 
 playsplash(); 
 }
  
 function OnTriggerExit(o:Collider)
 { 
 StopSplash(); 
 }
     
 function PlaySplash ()
 { 
 audio.loop = true;
 audio.clip = splashClip;
 audio.Play();
 }
     
 function StopSplash()
 audio.Stop()
 }
 
 
 
 
 
 
              Your answer