- Home /
Music Zones play music with tags?
I'm trying to make music zones, enter a music zone and it will play music according to the area. I want it all in one script. I figured out how to do it with tags, but I think I'm doing it terribly wrong :(. ALSO! It's supposed to play the music in the main camera. How can I do that?
#pragma strict
var MusicZone1Adventure : AudioClip;
function Start () {
}
function Update () {
}
function OnTriggerEnter() {
if (gameObject.tag == "MusicZone1Adventure") {
audio.PlayOneShot = MusicZone1Adventure;
}
}
Can anyone point out what I'm doing wrong? Thanks in advance!
Answer by T-D-M-3 · Apr 19, 2014 at 12:19 AM
The way that I usually do things like that i use this:
function Start () {
}
function Update () {
}
function OnTriggerEnter(hit:Collider){
if(hit.tag == "Player"){
//Whatever you need to do
}
}
Basically it activates the code as soon as anything with the Player tag hits it. so you could rig it to play the sound when the player reaches a certain point.
Yes, but that would make me have to make a new script for every music trigger. I'm trying to make it so all triggers have tags with their music name on it. When player goes into a trigger, the music according to the trigger's tag will play. Any suggestions towards that?
Okay, now i see what you mean. so what you need to do is this:
var Clip1 : AudioClip;
var Clip2 : AudioClip;
var Clip3 : AudioClip;
var Clip4 : AudioClip;
var Clip5 : AudioClip;
function OnTriggerEnter(hit:Collider){
if(hit.tag == "Clip1"){
audio.Stop();
audio.PlayOneShot(Clip1, 0.7);
}
if(hit.tag == "Clip2"){
audio.Stop();
audio.PlayOneShot(Clip2, 0.7);
}
if(hit.tag == "Clip3"){
audio.Stop();
audio.PlayOneShot(Clip3, 0.7);
}
if(hit.tag == "Clip4"){
audio.Stop();
audio.PlayOneShot(Clip4, 0.7);
}
if(hit.tag == "Clip5"){
audio.Stop();
audio.PlayOneShot(Clip5, 0.7);
}
}
@script RequireComponent(AudioSource)
just attach this script to the camera and when the player hits anything tagged with Clip1, clip2... it will play the corresponding sound. hope this helps!
Quick edit it needs to be attached to the Player object.
Your answer
Follow this Question
Related Questions
Music Zones play music with tags? 0 Answers
Stream music in Unity mobile 0 Answers
Audio Trigger 3 Answers
Audio Source will not "unmute" after toggling? 2 Answers
Unintended audio gaps 1 Answer