Audio repeats

hello i am having a problem with my gun . When i shoot it the audio is being repeated when i click the shoot button rather than being continued. Please help

public enum GunType { Semi, Burst, Auto };
public GunType gunType;
public float rpm;

// Components
public Transform spawn;
private LineRenderer tracer;

// System:
private float secondsBetweenShots;
private float nextPossibleShootTime;

void Start() {
    secondsBetweenShots = 60 / rpm;
    if (GetComponent<LineRenderer>())        {
        tracer = GetComponent<LineRenderer>();
    }
}

public void Shoot()  {

    if (CanShoot())
    {
        Ray ray = new Ray(spawn.position, spawn.forward);
        RaycastHit hit;

        float shotDistance = 20;

        if (Physics.Raycast(ray, out hit, shotDistance))
        {
            shotDistance = hit.distance;
        }

        nextPossibleShootTime = Time.time + secondsBetweenShots;

        GetComponent<AudioSource>().Play();

        if (tracer)  {
            StartCoroutine("RenderTracer", ray.direction * shotDistance);
        }
    }

}

public void ShootContinuous()  {
    if (gunType == GunType.Auto) {
        Shoot ();
    }
}

private bool CanShoot() {
    bool canShoot = true;

    if (Time.time < nextPossibleShootTime) {
        canShoot = false;
    }

    return canShoot;
}

IEnumerator RenderTracer(Vector3 hitPoint)    {
    tracer.enabled = true;
    tracer.SetPosition(0, spawn.position);
    tracer.SetPosition(1, spawn.position + hitPoint);

    yield return null;
    tracer.enabled = false;
}

}

I’m not sure, but you might want to try

GetComponent<AudioSource>().PlayOneShot();

instead of

GetComponent<AudioSource>().Play();

If not, is the audio clip only one shot or is it a continuous shooting sound?

@Kharief

I’m not sure I understand what you mean by

When i shoot it the audio is being repeated when i click the shoot button rather than being continued.

this line

GetComponent().Play();

Will start the sound again from the beginning. To pause and resume the audio from it’s current position you would need to enable and disable the audiosource instead of using play and stop.

@PeteD

@Sageose

i made a video about my problem here : - YouTube