- Home /
Question by
m1k3ym0053 · Jul 29, 2018 at 02:00 AM ·
fpsgun scriptconfused
I don't understand, the following coding is my recoil coding for c sharp.
void Update() { if (Input.GetButtonDown("Fire1")) { AudioSource gunsound = GetComponent(); gunsound.Play(); GetComponent().Play("GunShot"); } }
Comment
Answer by Carterryan1990 · Jul 29, 2018 at 05:54 AM
It should be
public AudioClip gunSound; //insert clip in the inspector
public AudioSource audio; //insert Audio Source Component in the inspector
void Update() {
if(Input.GetButtonDown("Fire1"))
{
audio.PlayOneShot(gunSound);
}
}
if you want to get fancy you can do this.
public AudioClip gunSound; //insert clip in the inspector
public AudioSource audio; //insert Audio Source Component in the inspector
void Update() {
if(Input.GetButtonDown("Fire1"))
{
audio.pitch = (Random.Range(0.8f, 1f)); //this will change the pitch each time its fired so each gun shot doesnt sound exactly the same
audio.PlayOneShot(gunSound);
}
}