- Home /
 
              This question was 
             closed Jul 14, 2018 at 12:11 PM by 
             MilkyPancack for the following reason: 
             
 
            Other
 
               Question by 
               MilkyPancack · Jul 08, 2018 at 03:36 PM · 
                audioaudiosourceaudioclipreloadplayoneshot  
              
 
              How to fix my reload sound?
My reload sound plays but it is delayed until after the gun has reloaded and it interferes with the firing sound. How do I make it play at the start?
using UnityEngine; using System.Collections;
public class Gun : MonoBehaviour {
 public float damage = 10f;
 public float range = 100f;
 public float fireRate = 15f;
 public int maxAmmo = 30;
 private int currentAmmo;
 public float reloadTime = 1f;
 public AudioClip reload1;
 private bool isReloading = false;
 AudioSource audio;
 private bool playReloadSound = false;
 public Camera fpsCam;
 public ParticleSystem muzzleFlash;
 public GameObject impactEffect;
 private float nextTimeToFire = 0f;
 public Animator animator;
 void Start ()
 {
     currentAmmo = maxAmmo;
     audio = GetComponent<AudioSource>();
 }
 
 void Update () {
     if (isReloading)
         return;
     if (playReloadSound)
         audio.PlayOneShot(reload1); // Reload sound
         playReloadSound = false;
     if (currentAmmo <= 0)
     {
         StartCoroutine(Reload());
         return;
     }
     if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
     {
         nextTimeToFire = Time.time + 1f / fireRate;
         Shoot();
         AudioSource shoot = GetComponent<AudioSource>();
         shoot.Play();
     }
 }
 IEnumerator Reload ()
 {
     isReloading = true;
     Debug.Log("Reloading...");
     animator.SetBool("Reloading", true);
     playReloadSound = true;
     yield return new WaitForSeconds(reloadTime);
     animator.SetBool("Reloading", false);
     currentAmmo = maxAmmo;
     isReloading = false;
 }
 void Shoot()
 {
     muzzleFlash.Play();
     currentAmmo--;
     RaycastHit hit;
     if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit))
     {
         Debug.Log(hit.transform.name);
         Target target = hit.transform.GetComponent<Target> ();
         if (target != null) 
         {
             target.TakeDamage (damage);
         }
         GameObject impactGo = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
         Destroy(impactGo, 2f);
     }
 }
     
 
               }
               Comment
              
 
               
              Follow this Question
Related Questions
PlayOneShot won't work. 1 Answer
Playing many audioclips with PlayOneShot causes all sound to cut out. 0 Answers
Sound wont play when triggered (javascript) 1 Answer
Why PlayOneShot is not working? C# 1 Answer
WWW Audio and PlayOneShot: Playing the same AudioClip Twice Cuts off First Instance 1 Answer