- Home /
Question by
Major-Idea · Aug 03, 2012 at 12:15 PM ·
triggersoundmusicstart
Trigger Start of Sound
Hey!
I have a level set up with various musics set along it. Everything is fine and they fade in and out correctly depending on the player position.
Trouble is, when the player gets to them, they're already halfway through.
I need a way to trigger the start of the music, but I have been unsuccessful so far.
Any help would be of great value!
Thanks
Comment
Answer by fafase · Aug 03, 2012 at 12:20 PM
Add an empty game object (if not already) and position it, add an AudioSource to your object and add the clip you want to play. Tick off the play on awake. Then add a sphere collider to your object and tick IsTrigger. Give it the size you want.
Add this script to it:
function OnTriggerEnter(other:Collider){
if(other.gameObject.tag =="Player")audio.Play();
}
Now the audio will play when you enter that sphere. You can also stop it using:
function OnTriggerExit(other:Collider){
if(other.gameObject.tag =="Player")audio.Stop();
}