hello and hello… i tried to do a script that plays footsteps sounds in every 1 secound or so, while the animation “walk” is being played, so heres my script
var STEP1: AudioClip;
var STEP2: AudioClip;
var STEP3: AudioClip;
var clock = 0;
function Update () {
if (animation.IsPlaying("walk")) {
clock ++;
if (clock > 10)
{
audio.clip = STEP1;
audio.Play();
}
if (clock > 40)
{
audio.clip = STEP2;
audio.Play();
}
if (clock > 80)
{
audio.clip = STEP3;
audio.Play();
}
if (clock > 85)
{
clock = 0;
}
}
}
yhea, i know my ways to keep track of time are very primitive, with that clock var, but i just cant get anyother way to work… time.time time doest work for me… and yeld wait i never undestand how it works… something about coroutines … (in fact i dont know what courotines are, too )
so … for me… keep track of time with this clock var is easier… and it aways worked for other things in my game.,… except this time
i made this script and attached to the player… but , when it plays the step sound… the sounds comes out all distorced… and weird… but if i stop walking, then the last step sound played comes out good
This might not be your problem but your if code is a bit messy. Say that clock = 81…well. it’s greater than 10, 40 and 80 meaning that audio.Play() is getting called 3 times in a row.
If I’m not mistaken, I think the 2D platform tutorial has a good section on footsteps you might want to look into.
heyyy!! totally!!.. you’re right… thats why the sound is coming out strange… its being played over and over one overlaping eachother…
ok… iwll check it out… and… well about attaching a colider to the foots… that may be a problem because the animation of walking sometimes make the character colide his foots on the wall lots of time (depending on how unflat the terrain is) so the sound may get a bit strange,… like a saloon dance if you know what i mean
besides… i wanted to make the sound diferent for each kind of floor… like if he steps on grass is a sound, if steps on concrete is another… you know… but it think i got it now… i just have to fix this overlap problem
One suggestion for your code. If you use clock++ to check when to change the sound, when you export your game to any plataform, you’ll have some problems with the framerate: in some computers (devices) it will be very slow and in others it will be very fast. The reason you don’t notice this when you’re editing is because UnityEditor keeps the framerate constant (you can change that in Edit/Project Settings/Time) no matter what you write in your code.
Use this instead of clock++
var clock : float = 0;
...
clock += Time.deltaTime;
if (clock >= 0.5)
{
/...