- Home /
Firing Sound
I have a firing script, as shown below. How do I add a sound that plays whenever the gun fires? It is a machinegun, so it needs to loop when you hold down the button.
var projectile : Rigidbody;
public var speed = 10;
public var destroyTime = 1;
function Update () {
if ( Input.GetButton ("Fire1")) {
clone = Instantiate(projectile, transform.position, transform.rotation);
clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed));
Destroy (clone.gameObject, destroyTime);
}}
Answer by Olgo · Jul 18, 2013 at 07:12 PM
Look in the related questions section of the bottom right of your question here, there are millions of examples. I've pasted one into your code. The only problem I see now is you'll be creating a bullet and playing the sound once per frame. That's a fast machinegun.
var projectile : Rigidbody;
public var speed = 10;
public var destroyTime = 1;
public var sound : AudioClip;
function Start(){
audio.clip = sound;
}
function Update () {
if ( Input.GetButton ("Fire1")) {
audio.Play();
clone = Instantiate(projectile, transform.position, transform.rotation);
clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed));
Destroy (clone.gameObject, destroyTime);
}
}
Answer by mineus64 · Jul 18, 2013 at 07:19 PM
Ok. Thx.
And, it works exactly as I like it. Thx thx thx thx thx.
If Olgo's reply answered your question, please mark it as "solved" so that he/she gets $$anonymous$$arma, and maybe a thumbs up! :)
Your answer
Follow this Question
Related Questions
How to add sound to gun shot script 4 Answers
How to create a light effect on weapon fire 0 Answers
hey here wesley again sound for my gun 1 Answer
Machine gun sound — better to loop individual bullets or have a longer sample? 0 Answers
Sound not playing correctly And projectiles not firing Correctly... 1 Answer