- Home /
Gunfire Sound Gets Cut Off When I Release the Aim Button. Help?
So for my game you have to hold down right mouse click to aim and then simultaneously click the left mouse click to shoot. When the gun is fired, I'd like the gunfire sound to play. Currently, I have this working, but I have a problem. When I let go of the aim button, the gunfire sound immediately stops playing (if it hasn't already played through).
I understand this is happening because of the placement of my PlayShootSound method call, but what I can't figure out is where to put it so that I don't have this problem.
Any pointers/ideas?
public class Weapon : MonoBehaviour {
public float fireRate = 0;
public float damage = 10;
public LayerMask whatToHit;
public Transform BulletTrailPrefab;
public Transform MuzzleFlashPrefab;
float timeToFire = 0;
float timeToSpawnEffect = 0;
public float effectSpawnRate = 10;
Transform firePoint;
public AudioClip shootSound;
private AudioSource gunFire;
bool isShooting = false;
// Use this for initialization
void Awake () {
gunFire = GetComponent<AudioSource>();
firePoint = transform.Find("FirePoint");
if (firePoint == null)
{
Debug.LogError("No fire point...Please make an empty object named FirePoint and attach to end of gun. :)");
}
}
void Update () {
CheckIfShooting();
}
void CheckIfShooting()
{
if (Input.GetButton("Fire2")) // Holding right click
{
if (fireRate == 0)
{
print("fire rate is 0");
if (Input.GetButtonDown("Fire1")) // Left click while holding right click
{
Shoot();
PlayShootSound();
}
}
else
{
if (Input.GetButton("Fire1") && Time.time > timeToFire) // For automatic guns
{
timeToFire = Time.time + 1 / fireRate;
Shoot();
PlayShootSound();
}
}
}
}
void Shoot()
{
Vector2 mousePosition = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
Vector2 firePointPosition = new Vector2(firePoint.position.x, firePoint.position.y);
RaycastHit2D hit = Physics2D.Raycast(firePointPosition, mousePosition - firePointPosition, 100f, whatToHit);
if (Time.time >= timeToSpawnEffect)
{
Effect();
timeToSpawnEffect = Time.time + 1 / effectSpawnRate;
}
// Debug.DrawLine(firePointPosition, (mousePosition - firePointPosition) * 100, Color.cyan);
if (hit.collider != null)
{
// Debug.DrawLine(firePointPosition, hit.point, Color.red);
Debug.Log("We hit " + hit.collider.name + " and did " + damage + " damage.");
}
}
void PlayShootSound()
{
gunFire.PlayOneShot(shootSound, 1f);
}
void Effect()
{
Instantiate(BulletTrailPrefab, firePoint.position, firePoint.rotation); // Fire Bullet
Transform muzzleFlashClone = Instantiate(MuzzleFlashPrefab, firePoint.position, firePoint.rotation) as Transform;
muzzleFlashClone.parent = firePoint;
float size = UnityEngine.Random.Range(6f, 9f); // Randomize size of muzzle flash
muzzleFlashClone.localScale = new Vector3(size, size, size);
Destroy(muzzleFlashClone.gameObject, 0.05f);
}
I found the answer finally!!!!
I followed this tutorial on how to create an audio manager and call sounds through it...and now my problem is fixed. :)
Answer by ToastandBananas · Jun 16, 2018 at 04:26 PM
I found the answer finally!!!!
I followed this tutorial on how to create an audio manager and call sounds through it...and now my problem is fixed. :)
Answer by crawniik · Jun 15, 2018 at 11:56 PM
It may not be the best fix, but you COULD make an IEnumerator method for yield return new WaitForSeconds(waitTime);
where waitTime is the same amount of time that the gunshot sound is, and then add a line under your PlayShootSound(); for WaitTime(); or whatever you want to name the wait method.
I'm a newb, so there may be better options out there that I just don't know about yet.