|
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);
}
}
(comments are locked)
|
|
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?
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)
|
|
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: 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...
Aug 07 '12 at 12:08 PM
Fattie
(comments are locked)
|
|
This script will do the job! 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 () { }
Aug 07 '12 at 11:45 AM
Seth Bergman
Yep, Good work :D
Aug 07 '12 at 11:47 AM
UnrealIzzy
(comments are locked)
|

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.