x


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);
    
  }
}
more ▼

asked Aug 07 '12 at 10:38 AM

Blink gravatar image

Blink
106 10 22 32

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.

Aug 07 '12 at 11:54 AM Fattie
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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.

more ▼

answered Aug 07 '12 at 11:19 AM

aldonaletto gravatar image

aldonaletto
42.5k 16 43 202

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?

Aug 07 '12 at 11:28 AM Blink

Ah..

you can either switch to : audio.PlayOneShot(footsteps);

or

audio.clip = footsteps;

audio.Play();

Aug 07 '12 at 11:36 AM Seth Bergman

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...)

Aug 07 '12 at 11:48 AM aldonaletto

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?

Aug 07 '12 at 12:01 PM Blink

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 (Mathf.Abs(Input.GetAxis("Vertical")) > 0.1){
      yield WaitForSeconds(waitTime);
      audio.PlayOneShot(footsteps[Random.Range(0,footsteps.length)]);
    }
    yield; // let Unity free till next frame
  }
}
Aug 07 '12 at 12:09 PM aldonaletto
(comments are locked)
10|3000 characters needed characters left

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...

more ▼

answered Aug 07 '12 at 11:18 AM

Seth Bergman gravatar image

Seth Bergman
7k 10 16 28

InvokeRepeating("sound", 0.5,0.5);
Aug 07 '12 at 12:08 PM Fattie
(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Aug 07 '12 at 11:39 AM

UnrealIzzy gravatar image

UnrealIzzy
64 8 8 13

this could be shortened considerably:

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;

}

Aug 07 '12 at 11:45 AM Seth Bergman

Yep, Good work :D

Aug 07 '12 at 11:47 AM UnrealIzzy
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5275
x542
x60

asked: Aug 07 '12 at 10:38 AM

Seen: 1544 times

Last Updated: 1 day ago