- Home /
Sound Doesn't trigger when firing projectile
I created a laser sound I want to trigger when a projectile is fired. The projectile launches fine and there is no error when I play the scene but i still cannot hear the sound.
using System.Collections; using UnityEngine;
public class ThrowObject : MonoBehaviour {
public GameObject projectile;
public AudioClip shootSound;
private float throwSpeed = 2000f;
private AudioSource source;
private float volLowRange = 0.5f;
private float volHighRange = 1.0f;
// Use this for initialization
void Awake () {
source = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown("Fire1"))
{
source.PlayOneShot(shootSound, 1F);
GameObject throwThis = Instantiate (projectile, transform.position, transform.rotation) as GameObject;
throwThis.GetComponent<Rigidbody>().AddRelativeForce (new Vector3 (0, 0, throwSpeed));
}
} }
For reference I am following this tutorial (https://www.youtube.com/watch?time_continue=1259&v=5N-428vKwaE) and I am stuck at the 20:48 minute mark. Even after tweaking in the Inspector my sound still does not play.
First off, you don't need both an AudioClip
and an AudioSource
; your AudioSource
already provides the AudioClip
as you showed in your editor screenshot. When you want to play the sound, just call source.Play();
. Second, and this is kind of silly and I assume your answer to both will be "yes", but
1) are you sure the audio is loud enough to hear? Try playing it in the editor to see if you can hear it, and 2) is the source close enough to the camera, such that falloff does not significantly affect volume?
Answer by Adarsh2303 · Jun 30, 2018 at 06:47 AM
@KamLook Just Restart Unity while you Earphone Connected.. I m not sure about this...But I always face this... I don't think your Script has a problem...
It still does not work after restarting. I can hear footsteps, jumping and landing in my head phones but not this projectile. The code for the footsteps and such was already created in the standard assets i downloaded. Below is a picture of the inspector for the throwing hand component if it helps.
[1]: /storage/temp/119856-pic-to-delete.png
Try Changing Sound
if not
Try...source.play(); in place of source.PlayOneShot(shootSound, 1F);
Answer by Grocho · Jun 30, 2018 at 08:41 AM
There are a couple things to look at here, 1st. Do you have multiple audio sources?
If so, you might want to make the Audio Source public an assign it yourself.
2nd. Your source's settings.
Post a screenshot of them so I can check it.
I also made the audio source public and assigned it to ThrowingHand but that did not work either.
Actually I do have two Audio sources, one for my player walking and moving and one for firing the gun. How would that interfere with my script above?
Your answer
Follow this Question
Related Questions
WWW Audio and PlayOneShot: Playing the same AudioClip Twice Cuts off First Instance 1 Answer
Play a segment in a audio file 1 Answer
AudioSource.IsPlaying returns true when only playing one shot? 0 Answers
How to fix my reload sound? 0 Answers
[Unity5] AudioSource PlayOneShot gives "Ran out of virtual channels" 0 Answers