- Home /
Need help on audio trigger.
Hi Guys, I am making a script to attach to my "is Trigger" Box Collider. but I am a bit stuck, I can't seem to get the audio to play when my character clicks his left mouse button while he is in the trigger zone???
var Trigger : AudioClip;
function OnTriggerEnter(){ //when he enters the trigger zone
if (Input.GetButtonDown ("Fire1")) { //if he presses the Fire1 Button
audio.Play(); //audio plays
}
}
I wan't the character to be able to "Left Click" and play the audio inside of the trigger zone??? Thanks :D
Answer by DannyB · Aug 04, 2012 at 07:52 AM
Let's eliminate problems first and handle the audio only.
Put a Debug.Log("here") inside tho IF condition, if you are not getting to it, the problem is not the audio.
If you are getting to it, and it just wont play (but with no error), then it is probably the settings of your Audio Source component. Make sure it is configured to be heard at a long distance (or at least at the distance you want it to be heard) by playing with the properties in he 3D Sound Settings area.
Thanks Alot!
I added the debug.Log and found it wasn't working properly so I changed the function from " function OnTriggerEnter" to " function OnTriggerStay " This way the player can activate it if he stays within the zone :D
Again Thanks =D
Excellent. I would also like to suggest a slightly modified approach, that adds a little more separation and control to your code.
OnTriggerEnter, set a variable canFire=true;
OnTriggerExit, set it to false.
Then, in your Update(): if( Input.GetButtonDown( "Fire1" ) && canFire ) audio.Play()
Answer by UnrealIzzy · Aug 04, 2012 at 08:16 AM
Final Code:
var Trigger : AudioClip;
function OnTriggerStay(){
if (Input.GetButtonDown ("Fire1")) {
audio.Play();
Debug.Log("audio is working");
}
}