- Home /
Playing Sound
Im using a C## script and Im wondering how to play sound when I press the H key.. it wont work and Im using a .wav sound file, nothing big. I cant get anything to play..
[RequireComponent(typeof(AudioSource))]
public class PlayerHealth : MonoBehaviour {
public AudioClip PlayerHit;
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.H)) {
audio.PlayOneShot(PlayerHit);
}
do you get any runtime errors with this or is there just no sound playing?
Add a debug line to see if the PlayOneShot method gets called. $$anonymous$$aybe your clip is marked as a 3D sound (default setting) and your listener is too far away.
Answer by khan-amil · Jan 21, 2014 at 01:31 PM
Assuming that's all, your code is good and should work.
Be sure to check taht your sound is either 2D or that your source is close to your Listener (default is on the main camera)
Answer by Drakulo · Jan 21, 2014 at 01:29 PM
If your sound is defined as "3D", playing it will considerate audio listener position in 3D space and apply volume changes. Have you tried to use 2D sound ? You can change it in the inspector when selecting your sound in the project window.
Your answer
Follow this Question
Related Questions
Random, 3d sound. 1 Answer
Play Audio Sound OnTriggerEnter 2 Answers
Set audio clip to 3D in script 1 Answer
[NoBraves] Sound makes lag 1 Answer
Audio Clip Plays 1 turn too late 1 Answer