- Home /
Problems in a footstep coroutine
Hello! I wrote a simple script that plays a random sound from an array if you have one of the arrow keys pressed down. This is supposed to simulate footsteps. The problem is that sometimes the sound for the footstep won't play. This seems to happen at completely random times. Take a look at the code and then I'll explain more.
var grassOne : AudioSource;
var grassTwo : AudioSource;
var grassThree : AudioSource;
var grassFour : AudioSource;
var walkSounds = new Array ();
var soundToPlay : int;
function Start(){
walkSounds[0] = grassOne;
walkSounds[1] = grassTwo;
walkSounds[2] = grassThree;
walkSounds[3] = grassFour;
StartCoroutine("Loop");
}
function Loop(){
while(true){
yield WalkSoundPlay();
}
}
function WalkSoundPlay(){
while(Input.GetButton("Horizontal") || Input.GetButton("Vertical")){
soundToPlay = Random.Range(0, 4);
walkSounds[soundToPlay].Play();
yield WaitForSeconds(.5);
}
}
Yesterday I figured out how to use the StartCoroutine command and a loop function to execute another function at predetermined time intervals. It worked perfectly in my applyDamage code. Here's how I understand this code should work. When the script is loaded, it sets the first four values of an array to be four different AudioSources. It also starts the coroutine called "Loop". (Question: Using the StartCoroutine keyword executes the code in the specified function. I understand that if you called "Loop();" in the Start function it would only execute once, but what's the difference between using StartCoroutine in the Start function and calling "Loop();" in the Update function?) Then, the while loop repeatedly executes the WalkSoundPlay function. (Question: I'm assuming that the while loop checks if it's conditions are true every frame. Is this true?) Every frame(?), the WalkSoundPlay function checks if your arrow keys are held down. If they are it sets soundToPlay to a random number between 0 and 3 (because when you use Random.Range with an integer, the max number is exclusive but the min number is inclusive right?). It then accesses the value in the walkSounds array that matches the number we just generated and since that value is an AudioSource, it plays it. Finally it tells the script that it has to wait .5 seconds before it can execute again. This is from the viewpoint of a 14 year old who has only been programming for a couple of months. The script looks really simple but I can't figure out what is causing the problem. here's what I have tried already:
Decreasing the WaitForSeconds time to .3
Increasing the WaitForSeconds time to 1
Making a new boolean called playSounds. In the update function, I wrote: If the arrow keys are held down set playSounds to true, else set it to false. Then under the WalkSoundPlay while loop's conditions instead of what's there now I wrote "playSounds == true". This had no effect on the script but I deleted it because I didn't want to use the update function if i didn't have to.
Getting rid of the Loop function and any references to it, then calling "WalkSoundPlay();" in the update function. This is what I tried to do before I knew about the StartCoroutine command. It still doesn't work.
If anybody knows a fix for this, that would be fantastic. Thank you for taking the time to read my post!
i can't see y that isnt working but since u said something about random times. maybe it has to be with ur random, maybe for some weird reason it just doesn't play if u get the same number in ur random soo u could try this. so u won't get the same number in row
if (soundToPlay == soundToPlay && soundToPlay != 3) { soundToPlay+=1; }else{ soundToPlay = 0; }
Answer by Mander · Aug 21, 2012 at 08:43 PM
i can't see y that isnt working but since u said something about random times. maybe it has to be with ur random, maybe for some weird reason it just doesn't play if u get the same number in ur random soo u could try this. so u won't get the same number in row. place this after this line `soundToPlay = Random.Range(0, 4);`
if (soundToPlay == soundToPlay&& soundToPlay != 3)
{
soundToPlay+=1;
}else{
soundToPlay = 0;
}
Your answer
Follow this Question
Related Questions
changing a coroutine's argument at the coroutine's runtime? 1 Answer
Coroutine gets skipped in javascript/unityscript 0 Answers
Stop coroutine not working 1 Answer
Execute code for a certain amount of time 1 Answer
Keeping track of time? 4 Answers