Trying to get full-auto fire for a gun in my game.
Hey all, so in my game the gun that you use is an AK-47 rifle and I want it to have a rapid fire sound effect whenever the player shoots. I have a script for this and a sound effect, but when I fire it doesn't sound like full auto rifle fire. It is just the audio clip repeating over and over again, sounding almost like burst fire. How do I make it so the effect can emulate full auto fire?
Script attatched below. using UnityEngine;
public class Gun : MonoBehaviour {
public float damage = 10f;
public float range = 100f;
public GameObject impactEffect;
public float fireRate = 15f;
public Camera fpsCam;
public ParticleSystem muzzleFlash;
private float nextTimeToFire = 0f;
bool didFire;
private AudioSource mAudioSrc;
// Update is called once per frame
void Update() {
if (Input.GetButton("Fire1"))
{
if (nextTimeToFire <= 0.5f)
{
nextTimeToFire += Time.deltaTime;
if (!didFire)
{
Shoot();
mAudioSrc.Play();
didFire = true;
}
}
else
{
didFire = false;
nextTimeToFire = 0;
}
}
else
{
didFire = false;
nextTimeToFire = 0;
}
}
void Start()
{
mAudioSrc = GetComponent<AudioSource>();
}
void Shoot ()
{
muzzleFlash.Play();
RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
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);
}
}
}
Thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220612222302im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Precise metronome problem 1 Answer
Stop Audio Source Clip while button Press 1 Answer
can i make a loop for each with all audiosources of my project ? 0 Answers
Is it possible to export multiple audioclips into one single wave file? 1 Answer
Footstep sound does not finish playing, it keeps restarting on movement 0 Answers