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);

}}

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);
     
    }
}

Ok. Thx.

And, it works exactly as I like it. Thx thx thx thx thx.