- Home /
Footsteps?
I have wrote this script hoping that it would play my footstep sound when w or s is held down but I have a lode of errors, is there any easier way to do this or am I nearly there?
var footsteps : AudioClip; var waitTime = 0.5;
function Update () {
if(Mathf.Abs(Input.GetAxis("Vertical")) > 0.1){ yield WaitForSeconds(waitTime); audio.Play(footsteps);
} }
If you are a beginner, never, ever ever use anything like yield, waitfor, or coroutines. (for that matter, there are very few, probably No, situations where you should use or even know about Update.)
In any event, never ever use yield, etc, if you are at this level.
Invoke and InvokeRepeating are the basic commands used in making video games.
You will need to learn how to use them and you should do that now. They are extremely simple.
Answer by aldonaletto · Aug 07, 2012 at 11:19 AM
You can't use yield inside Update or any other periodic routine (LateUpdate, FixedUpdate, OnGUI), but a very similar approach can work if started at Start (yes, Start can use yield!):
var footsteps : AudioClip; var waitTime = 0.5;
function Start () { // create an infinite loop that runs every frame: while (true){ if (Mathf.Abs(Input.GetAxis("Vertical")) > 0.1){ yield WaitForSeconds(waitTime); audio.Play(footsteps); } yield; // let Unity free till next frame } } This implements a kind of private Update: the code inside the while infinite loop is executed every frame, then the yield instruction lets Unity free to take care of its business until next frame.
Ive had this error since I started coding the sound it goes "No appropriate version of 'UnityEngine.AudioSource.Play' for the argument list '(UnityEngine.AudioClip)' was found." Its on line 9, whats going wrong here?
Ah..
you can either switch to : audio.PlayOneShot(footsteps);
or
audio.clip = footsteps;
audio.Play();
Oh, I have not noticed the wrong use of Play: weirdly, the Play argument is a delay expressed in number of samples, not the AudioClip reference or name - this is very unintuitive, and almost everybody makes this mistake when starting using Unity (me included...)
This works perfectly now thanks, just one more thing how could the script be changed so that it plays say 3 different footstep sounds randomly, so it doesn't get repetitive?
Use an AudioClip array, populate it in the Inspector and select the sound with Random.Range:
var footsteps: AudioClip[]; // drag the sounds here var waitTime = 0.5;
function Start () { // create an infinite loop that runs every frame: while (true){ if ($$anonymous$$athf.Abs(Input.GetAxis("Vertical")) > 0.1){ yield WaitForSeconds(waitTime); audio.PlayOneShot(footsteps[Random.Range(0,footsteps.length)]); } yield; // let Unity free till next frame } }
Answer by Seth-Bergman · Aug 07, 2012 at 11:18 AM
you can't use WaitForSeconds in Update, because it's called every frame, but otherwise this should work, just remove that line..
but if you really want the pause, you can create a coroutine to call from Update:
var footsteps : AudioClip;
var waitTime = 0.5;
function Start(){
audio.clip = footsteps;
}
function Update () {
if(Mathf.Abs(Input.GetAxis("Vertical")) > 0.1)
WaitFootsteps();
else
makeSound = false;
if(makeSound)
audio.Play();
}
function WaitFootsteps(){
if(!makeSound){
yield WaitForSeconds(waitTime);
makeSound=true;
}
}
this should pause before the FIRST step, I assume that's what you wanted.. If you're just trying to pause between EVERY step, just loop the clip and make it have a half-second pause in the actual audio clip, that would make more sense...
Answer by UnrealIzzy · Aug 07, 2012 at 11:39 AM
This script will do the job!
var Walk : AudioClip;
function Update () {
if (Input.GetKey("w") || Input.GetKey("a") || Input.GetKey("s") || Input.GetKey("d"))
{
if(!audio.isPlaying) // this line may not even be necessary
audio.Play();
audio.loop = true;
}else
audio.loop = false;
}
When you press either w,a,s or d then it will play the sound you decide to play. Hope this helped :D
this could be shortened considerably:
function Update () {
if (Input.Get$$anonymous$$ey("w") || Input.Get$$anonymous$$ey("a") || Input.Get$$anonymous$$ey("s") || Input.Get$$anonymous$$ey("d"))
{
if(!audio.isPlaying) // this line may not even be necessary
audio.Play();
audio.loop = true;
}else
audio.loop = false;
}
Answer by DaveA · Feb 09, 2014 at 03:33 AM
Just grab this asset: http://u3d.as/content/qlc/advanced-footstep-system/6o2