- Home /
Question by
tss · Mar 28, 2013 at 03:22 PM ·
javascriptaudioprogrammingsound
play sound when moving stop sound when not
I am working on a tank game but whenever i use this code and move around a lot it starts bugging out and plays weird parts of the audio. can someone ether give me a reference or add on to my script. Thank you for all your help unity answers.
function Update()
{
if(Input.GetButtonDown("Vertical"))
{
audio.Play ();
}
if (Input.GetButtonUp("Vertical"))
{
audio.Pause();
}
}
and another script for moving up and down
function Update()
{
if(Input.GetButtonDown("Horizontal"))
{
audio.Play ();
}
if (Input.GetButtonUp("Horizontal"))
{
audio.Pause();
}
}
Comment
I don't have an answer for you, but just to be clear about the question: these are two separate audio sources on two separate game objects?
Best Answer
Answer by $$anonymous$$ · Mar 28, 2013 at 10:19 PM
try
function Update()
{
if(Input.GetButton("Vertical")
{
if(!audio.isPlaying)
{
audio.Play()
}
}
else
{
audio.Stop();
}
if(Input.GetButton("Horizontal")
{
if(!audio.isPlaying)
{
audio.Play()
}
}
else
{
audio.Stop();
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Audio Needs to stop 1 Answer
Can't get a sound to loop 2 Answers
Sound playing at random. (JS) 2 Answers
play sound while at certain coordinates of map (javascript) 1 Answer