How to Write a Sound Script

I have a sound effect of footsteps, and I want it to play whenever I press W,A,S, or D…anyone able to help me?

  • Upload an audio file like an mp3 or ogg file.
  • Attach an audiosource component to your entity.
  • Add the audio asset to your audio source component
  • Add a script component to your entity
  • Create a new script and assign it to the script component
  • In your scripts call: this.entity.audiosource.play("sound name");
  • ???
  • PROFIT!

So this will play when I press W,A,S,D? I don’t want it just to play all the time.

var playing = false;
if (!playing && app.keyboard.isPressed(pc.KEY_W) {
    this.entity.audiosource.play("my sound");
    playing = true;
} else if (playing) {
    this.entity.audiosource.stop();
}

Something like this for handling the W key

Ok thank you! I’ll see if that works.

And how would you go about playing different footsteps sounds depending on the type of ground you are walking on; concrete, gravel, wood, grass, snow etc.

I would have it play at intervals when the user is moving and the timing between footsteps or even the sound effect themselves may change pending on the play speed/what they are walking on.