- Home /
Trying to loop audio in a pattern - after 10 seconds under condition
I am trying to write a function where if the character controller in question is moving, it will play a sound every 10 seconds. For some reason the code keeps on looping the audio even when loop is not selected in the audiosource for the controller. Any tips?
#pragma strict
var obj1 : CharacterController;
function Start () {
}
function Update () {
Ping();
}
function Ping(){
if(!audio.isPlaying && obj1.velocity != 0 )
{
audio.Play();
yield WaitForSeconds(10);
}
}
Answer by FreeTimeDev · Jul 25, 2013 at 04:51 AM
Instead of seeing if audio.isPlaying I'd create a new bool and turn that on/off for desired effect.
#pragma strict
var obj1 : CharacterController;
var canPlay : bool = true;
function Update () {
Ping();
}
function Ping(){
if(canPlay && Mathf.Approximately(obj1.velocity, 0)
{
audio.Play();
canPlay = false
yield WaitForSeconds(10);
canPlay = true;
Debug.Log("canPlay = true");
}else
{
Debug.Log("!canPlay || !Moving");
}
}
Also, since velocity is a float you'd probably want to compare it using Approximately. Comparing floats (even 5f/5f == 1f) can sometimes fail.
Your answer
Follow this Question
Related Questions
Getting audio to play on collision 3 Answers
Sound on collision 3 Answers
Setting Scroll View Width GUILayout 1 Answer
Boolean and audio is not working right 1 Answer
Change Pitch on Collision 1 Answer