- Home /
Wait Until Sound is Finished
So I have a Custom Weapon with Custom Animation attached to my player. When the player Left-Clicks it plays the animation and sound. However if the player repeatedly left clicks the sound restarts over and over again interrupting itself. How can I tell the engine in JavaScript, to wait a certain amount of time before playing the sound again? I'm using the sound attached to the weapon as a AudioSource Component. Here is the code I'm using to play the animation and sound.
function Update () {
if(Input.GetButtonDown("Fire1"))
{
animation.Play("Punch");
audio.Play();
}
}
you just use Invoke to call a "callback" that let's you know it is finished. sadly you just have to manually insert the known time of the sound
Answer by fafase · Jun 15, 2012 at 08:09 PM
function Update () {
if(Input.GetButtonDown("Fire1")&& !animation.isPlaying)
{
animation.Play("Punch");
audio.Play();
}
}
It will check if the button is pressed AND if the animation is not playing. If it is still paying it returns false, no action.
Edit: You have the same members variable for audio.isPlaying. Note it is not a function so no ()
your suggestion worked perfectly now my Weapon is animating and playing sound as it should. Thank you very much good sir or madam!
Answer by Jessica_Ann · Jun 15, 2012 at 07:46 PM
You probably want to use Wait for seconds. It stops and waits for a passed in amount of time.I linked the scripting help for you. Hope this helps.
Well,....Problem would be that every time you would press the button it would not keep from acting but just wait for x seconds, meaning that if you press 10 times, you will have 10 actions after x seconds. Or you would have to add a boolean.
Your answer
Follow this Question
Related Questions
how to make make a game wait for 10 seconds the change level 2 Answers
How to open a .unity3d file 1 Answer
how to lock screen? 1 Answer
audio.Play ignoring WaitForSeconds 2 Answers
CREATING A TERRAIN 3 Answers