- Home /
What is the script code on how to trigger a song for the player to Listen to?
What is the code for the player to collide with and trigger a music source to the player to hear as an Audio Source not at the current position. Because I am having difficulty making the player collide with the trigger and everything would work but there would be no direct sound for the player to listen to. SO if you could help I would really appreaciate it. ALOT!
By "not at the current position" do you mean you want to trigger a sound somewhere else in the scene?
Just a note: you don't need to post your question again if it isn't immediately answered, its still on the site so reposting it just kind of clutters.
Yes to where you first start off by not hearing any music play untill you reach a certain point of the level $$anonymous$$usic would start to play like if it were HALO or somethin.
Answer by xToxicInferno · Feb 28, 2010 at 04:13 AM
I am not sure if this is what you wanted but here is what i got out of it, I decided to add comments to make things easier for you.
// This is a public variable so you have to assign the audio clip you wish to play in the Inspector var HaloTheme : AudioClip;
// Make a box, delete the mesh renderer, and make the Box Collider a trigger by selecting 'Is Trigger'
// Then expand it to enclose the whole area you wish it to cover, ie a room.
function OnTriggerEnter (other : Collider) {
// Make sure your character is tagged 'player'
if (other.gameObject.tag == "player") {
// Plays the audio that you want...
audio.Play(HaloTheme);
}
}
// Simply makes it so the object can play audio
@script RequireComponent(AudioSource)
Also, i didn't put a comment in there for it, but on the Audio Source comopnet that is added, make sure you have it set to Loop, if you want it to loop that is.
Lastly, take a look at this for other Audio related scripting issues: Unity Scripting Reference: Audio Source
Hopefully i have helped out, good luck and have fun with Unity!!
Answer by StephanK · Feb 27, 2010 at 11:47 AM
Not sure if I understand your question, but if you want to start a jukebox when a player is entering the room the only thing you have to do is attach a trigger collider to the jukebox that contains the room or area where the music should be heard. Then OnTriggerEnter start the AudioSource. If the clip attached to the AudioSource is marked as a 3d sound the music's level will depend on the distance of the AudioListener (probably attached to your cam/player) to the AudioSource. If you don't want that disable the clip's 3d sound property.