[SOLVED] How to add walking sound effect?

I have the .wav file for walking, I just need to add it when player clicks WASD, is there any working example I can look at? AND is there a playcanvas way to do it, I suppose this can be done in multiple ways so is there a way that playcanvas recommends? also I have looked at the tutorials, the sound tutorial is broken I think, it just doesn’t play any sound ( I checked all sound settings )

Hi @sir_akc,

Which tutorial isn’t working for you? For sound to start playing most browsers require the user to interact with the page first, to avoid spamming our ears.

This one works for me as soon as I click on the iframe:

https://developer.playcanvas.com/en/tutorials/basic-audio/

To your question, the way I’d do it:

  1. Add an audio listener component to my player entity.
  2. Add a sound component and create the sound slot with the sound asset.
  3. In code I’d monitor the move/velocity vector if it has a length > 0, if that’s true I’d start playing the sound. Otherwise I’d stop.
3 Likes

is there a way to see that project code?

Here you go: https://playcanvas.com/project/405821/overview/tutorial-basic-audio

I think it was meant to be in the example page as well, the link seems to be missing.

2 Likes
  1. In code I’d monitor the move/velocity vector if it has a length > 0, if that’s true I’d start playing the sound. Otherwise I’d stop.

Maybe you can sychronize the pitch of the sound to that value, more stepping if you go faster …

2 Likes

hey how do you check for velocity? I am using the default tutorial from the playcanvas

okay that is a good suggestion, I put a simple if statement to check like this:

if(this.entity.rigidbody.linearVelocity.lengthSq() > 0.0001)
    {
        this.entity.sound.play('walker');
    }

but the code makes the sound play again and again, I put this code in the update method, so its checking for that over and over again, I am thinking to use a bool to check if already playing or not

OKAY, FOUND THE PERFECT CODE:

just make sure that you set the footstep sound on loop

2 Likes

Nice, thanks for sharing your solution @sir_akc!