- Home /
Horrible footstep looping problem
Hi, I'm trying to play random footsteps at normal intervals but each time I move in my game I can hear a strange looping sound like if the random sounds were playing non-stop. I can't find the solution to my problem. Help would be greatly appreciated, thanks.
var audioSources : AudioClip[];
function Walk()
{
if ( Input.GetAxis( "Horizontal" ) || Input.GetAxis( "Vertical" ) )
{
yield WaitForSeconds (1);
var nextClip = audioSources[Random.Range(0, audioSources.Length)];
audio.clip = nextClip;
audio.Play();
}
}
function Update()
{
Walk();
}
Answer by AmazingDeveloper2 · Aug 29, 2014 at 04:05 PM
Here is my code for this. Hope it helps
void Update ()
{
if(Input.GetKey(KeyCode.W) ||
Input.GetKey(KeyCode.A) ||
Input.GetKey(KeyCode.S) ||
Input.GetKey(KeyCode.D))
{
if(!audio.isPlaying) audio.Play();
}
else
audio.Stop();
}
Well my footsteps aren't random anymore. I am trying to switch between 4 different footsteps. I think the problem is because of my update function. I don't know what to do, thanks though.
When you call audio.Play() in every frame inside Update function, it begins playing from the very beginning. Try to use my code with one audio of the footstep for beginning.
Answer by no9rg · Sep 04, 2014 at 01:46 PM
Tweaking a little your script:
var audioSources : AudioClip[];
var playStep : boolean;
function Walk()
{
if ( ( Input.GetAxis( "Horizontal" ) || Input.GetAxis( "Vertical" ) ) && playStep )
{
playStep = false; // to disallow repeating the steps to often
var nextClip = audioSources[Random.Range(0, audioSources.Length)];
audio.clip = nextClip;
audio.Play();
yield WaitForSeconds (1);
playStep = true; // now you can allow to play next step
}
}
function Start () {
playStep = true; // to allow the first step to play
}
function Update()
{
Walk();
}
Your answer
Follow this Question
Related Questions
Problem with seamless music looping in Unity 5 1 Answer
Sound pause on audio loop? 2 Answers
Random footsteps 5 Answers
Strange Bug or Code Error with footsteps? -1 Answers