- Home /
How do I call audio once when conditions are met?
I need an audio clip to play once every-time the player gets to far from his base. The problem is if it's in an update function then it happens several times and if it's in a start or awake function it only happens at the start of the game. What's the proper way to get it to call once every time the conditions are met?
Answer by Peter G · Mar 06, 2011 at 07:31 PM
You can either put a conditional in an Update()
which wouldn't be too hard.
You could use InvokeRepeating to decrease the frequency which you check:
var frequency = .5; //Check every half second.
function Start () { InvokeRepeating("CheckPlayerDist", frequency, frequency); }
function CheckPlayerDist() { if(PlayerDistance > someDistance) { audio.PlayOneShot(someSound); } }
or you could put a large trigger around your base and then play a sound when the player leaves it:
function OnTriggerExit(col : Collider) {
if(col == player) {
audio.PlayOneShot(someSound);
}
}
Awesome, it seams like I learn these things then forget half of em ;) I appreciate the help.
Your answer
Follow this Question
Related Questions
Random Audioclip. No Repeat ?? 1 Answer
Audio is repeating. 3 Answers
On Mouse Click Play Sound 1 Answer
There is no 'audioSource' attached to the game object error 1 Answer