- Home /
 
 
               Question by 
               inaudiblefuzz · Feb 07, 2019 at 01:43 AM · 
                c#audioscripting beginner  
              
 
              Adding audio to gun shot
I'm working on my first gun script and I was wondering how I would go about adding sound? I have added an Audio Source to the gun and imported my sound. Then do I add a public AudioClip with the files name? How would I get the sound to trigger when firing?
Thanks!
using UnityEngine;
 public class Gun : MonoBehaviour
 {
 
     public float damage = 10f;                  //Damage
     public float range = 500f;                  //Range of bullet
     public float fireRate = .50f;               //Greater fire rate = less time between shots.
     public float impactForce = 30f;             //Impact (force)
     public ParticleSystem Rail;                 //Particle System (Muzzle)
     public Camera fpsCam;                       //Camera. Click and drag off of hierarchy
     public GameObject impactEffect;             //Prefab system for impact
     
 
     private float nextTimeToFire = 0f;          //Instant shot (no lag when pressing down)
 
 
 
 
 
 
 
 
 
 
 
     void Update()
     {
 
 
 
         if (Input.GetButtonDown("Fire1") && Time.time >= nextTimeToFire)
         {
             nextTimeToFire = Time.time + 1f / fireRate;
             Shoot();
 
 
 
         }
 
     }
             
 
        
 
     void Shoot()
     {
 
         Rail.Play();                      //Play muzzle / barrel
 
         RaycastHit hit;
         if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
 
         {
 
           
 
             Target target = hit.transform.GetComponent<Target>();
             if (target != null)
             {
                 target.TakeDamage(damage);
             }
 
             if (hit.rigidbody != null)             //Hit RIGIDBODY component
             {
                 hit.rigidbody.AddForce(-hit.normal * impactForce);
             }
 
             GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
             Destroy(impactGO, .25f);  // Destroy impact effect 
             {
                
             }
 
         }
     }
 }
 
 
 
 
              
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by carl010010 · Feb 07, 2019 at 03:26 AM
Youll need to get a reference to your audio source in your source function. And then when you fire your gun play a sound. Heres an example of how i would do it.
  AudioClip[] m_GunShotSounds; //So the shots don't sound the same every time
 private AudioSource m_AudioSource; //The thing to play the audio
 
 private void Start()
 {
         m_AudioSource = GetComponent<AudioSource>();
 
         if (m_AudioSource == null)
         {
             Debug.LogError("No AudioSource found");
         }
 }
 
 void PlayShootingSound()
 {
         //Get an audioClip
         int n = Random.Range(1, m_GunShotSounds.Length);
         m_AudioSource.clip = m_GunShotSounds[n];
         //If you need to change the volume
         m_AudioSource.volume = volume;
         //Play the sound once
         m_AudioSource.PlayOneShot(m_AudioSource.clip);
 
 
         // move picked sound to index 0 so it's not picked next time
         FootstepSounds[n] = FootstepSounds[0];
         FootstepSounds[0] = m_AudioSource.clip;
 }
 
               void Shoot() {
      Rail.Play();                      //Play muzzle / barrel
      PlayShootingSound(); //Play a shooting sound
      //... the rest of your code
  }
 
               }
Answer by $$anonymous$$ · Feb 07, 2019 at 03:15 AM
The easiest way would be to put the script on to a gameobject, and then directly select the sound file from the audiosource component.
Your answer