- Home /
footstep sound
Hello ,i nead help to play the sound of footstep ,i wrote this code but it looks like it's playing in a loop .i just want to play the sound one evry the file length
float vertical = Input.GetAxis("Vertical") * movementSpeed *deltaTime;
float horizontal = Input.GetAxis("Horizontal")* turningSpeed *signe*deltaTime;
float jump=jumpForce;
if(vertical!=0||horizontal!=0)
{
mouvement=true;
}
else
{
mouvement=false ;
}
if(mouvement)
{
audio.PlayOneShot(WalkLeftSound);
}
I'm fairly sure you cannot use audio.isPlaying for audio.PlayOneShot , as PlayOneShot creates a separate gameObject that destroys itself after playing once (is not loopable). You would have to store a reference to the instantiated PlayOneShot gameObject and then check if it was null (had destroyed itself).
All you need to do is add a timer.
Example :
var timer : float = 0.0;
var timer$$anonymous$$ax : float = 2.0;
function Start()
{
timer$$anonymous$$ax = WalkLeftSound.Length;
}
function Update()
{
// all the previous code
if(mouvement)
{
timer += Time.deltaTime;
if ( timer >= timer$$anonymous$$ax )
{
timer = 0.0;
audio.PlayOneShot(WalkLeftSound);
}
}
}
Answer by ExpiredIndexCard · Apr 18, 2013 at 01:35 AM
Try using if (!audio.isPlaying) audio.PlayOneShot (WalkLeftSound);
Your answer
Follow this Question
Related Questions
Is there a way to create a random Audiosource loop? 2 Answers
Multiple audioSources on same gameObject? 1 Answer
Max amount of simultaneous, hearable sounds? 1 Answer
Music works but not when accessed by another script in JavaScript 1 Answer
Audio cuts out once a certain number of sounds are played 0 Answers