Question by 
               inaudiblefuzz · Feb 07, 2019 at 01:42 AM · 
                c#scripting problemaudioc# tutorial  
              
 
              Adding a sound to my Gun Script
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
              
 
               
              Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                