x


FPS Audio Walking Script

Hi, does any one know where I'v gone wrong here. Iv got is so when I press W, A or S my audio plays and it doesn't matter if I press two at the same time it still plays the audio. The only problem I have now is the audio Loops too fast is there any way of stopping this?

var sounds : AudioClip;
var keyDown = false;


function update() {

   if((Input.GetKey("w"))||(Input.GetKey("a"))||(Input.GetKey("d"))) {
    audio.clip = sounds;
      audio.Play();
      audio.loop = true;
      keyDown = true;    
   }else{
    keyDown = false;
    audio.clip = sounds;
    audio.loop = false;
   }

}

Thanks

more ▼

asked Dec 04, 2010 at 02:39 PM

onlyone gravatar image

onlyone
13 6 4 8

(comments are locked)
10|3000 characters needed characters left

2 answers: sort newest

i would think if you put in a scheduled timer, like the yield WaitForSeconds(audioStepLength); that would be what you are looking for and just mess around with the time till you got it where you want it.

I'm new to Unity and haven't got into coding anything yet, but i'm sure you can set a timer to wait to begin the loop again.

more ▼

answered May 08, 2011 at 07:32 AM

Fido77 gravatar image

Fido77
1

(comments are locked)
10|3000 characters needed characters left

Check out the FPS tutorial for a walk through... If anyone else can go through your script and actually see what to do, then I'd wait for that. But until then, see what you can see on your own...

Check the FPSPlayer script, they use an array of walking sounds to vary it up.

function PlayStepSounds () {
    var controller : CharacterController = GetComponent(CharacterController);

    while (true) {
        if (controller.isGrounded && controller.velocity.magnitude > 0.3) {
            audio.clip = walkSounds[Random.Range(0, walkSounds.length)];
            audio.Play();
            yield WaitForSeconds(audioStepLength);
        } else {
            yield;
        }
    }
}

And it is called during the awake function... See if that helps.

Link to the tutorial: http://unity3d.com/support/resources/tutorials/fpstutorial.html

All the best!

more ▼

answered Dec 05, 2010 at 02:58 AM

Justin Warner gravatar image

Justin Warner
6.7k 44 47 89

(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:

x4777
x1638
x1602
x126

asked: Dec 04, 2010 at 02:39 PM

Seen: 4564 times

Last Updated: Dec 04, 2010 at 02:39 PM