x


Audio Toggle Background Music (With Trigger )

I have a trigger that changes my camera into a new scene. My current code for the background music is this:

var Sound : AudioClip;

 function OnTriggerEnter(){
 audio.PlayOneShot(Sound);
 }

It's very simple. I would like to use my trigger to turn off this music and start another. That doesn't seem too difficult, it's when I leave that trigger. I would like to keep the main Background music and have it turn on again when you exit the trigger, and turn off the trigger music. (so toggle them.)

This is what I have so far, but I'm not sure if I'm on the right track.

var Sound1 : AudioClip;
var Sound2: AudioClip;

 function OnTriggerEnter(){
 audio.PlayOneShot(Sound1);

   if (OnTriggerEnter("CabinV1 Trigger"))
    {
     audio.Pause(Sound1);
     audio.PlayOneShot(Sound2);
    }

    If (OnTriggerExit("CabinV1 Trigger"))
    {
        audio.PlayOneShot(Sound1);
        audio.Pause(Sound2);
    }

}

I'm fairly new to this but I hope this was enough information. Please help!

Thank you!

more ▼

asked Dec 02 '10 at 09:44 PM

Heather gravatar image

Heather
97 5 5 12

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

I know exactly what you want. Checkout the question about audio. You might also want to see the script reference on OnTriggerExit

here for the question

click here for the reference

So you can do something like.

    var Sound1 : AudioClip;
    var Sound2 : AudioClip;
   function Start()
{
audio.Play(Sound1);
}

 function OnTriggerEnter(other:Collider)
    {
if(other.gameObject.tag == "CabinV1 Trigger")
{
audio.Pause(Sound1);
audio.Play(Sound2);
}
}


function OnTriggerExit(other:Collider)
{
if(other.gameObject.tag == "CabinV1 Trigger")
{
audio.Pause(Sound2);
audio.Play(Sound1);
}
}

Just make sure you have an audio listener attached to your player.

more ▼

answered Dec 11 '10 at 02:51 AM

JesusChristChangedMe gravatar image

JesusChristChangedMe
479 11 14 23

Thank you! You made my day.

Jan 14 '11 at 03:52 AM Heather
(comments are locked)
10|3000 characters needed characters left

May I ask where I would put this code please? I am looking for something very similar :)

Many thanks, Adam

more ▼

answered Jul 16 '11 at 03:31 PM

Spence89 gravatar image

Spence89
1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5093
x1030
x986
x262
x162

asked: Dec 02 '10 at 09:44 PM

Seen: 2156 times

Last Updated: Jul 16 '11 at 03:31 PM