I would Like to know how to code for playing a sound on a button Click R
thank you
Project Editor; https://playcanvas.com/editor/scene/1324849
Project; PlayCanvas 3D HTML5 Game Engine
Hi @Jack101! The code will look something like the code below. Please check also the manual page to see what else you need to play a sound.
// update function
if (this.app.keyboard.wasPressed(pc.KEY_R)) {
this.entity.sound.play('Name Of Your Sound Slot');
}
Would you attach this script to the player and add the sound then put that script to the player with the music?
You can do it in many ways, but to keep it simple I would indeed add both to the same entity.
ok thank you i will see if it works
would you put this in the initialize or by itself
it seems your code has an error
Yes, you are right. I have fixed the above code.
ok thank you ill see if it works
so i can’t hear the sound is this correct how it should be
Yes, that seems to be correct. Did you add a AudioListener component to one of your entities?
yes
Alright, good. Did you add the script to the entity?
hold up my friend keeps taking up the launch screen with his settings menu give me a minute please and i will test it
yes i forgot to add it to the player but it still wont play the sound
Can you share a link of your project please?
https://playcanvas.com/editor/scene/1324849
or this is the main PlayCanvas 3D HTML5 Game Engine
we would like the sound to play when you hit the space bar