New to Unity. Very new. I am trying to make a script for my gun to fire bullets and have a muzzle flash when it does so. It works… but sometimes bullets just disappear after going a small (0.1-0.2 units on x) distance from the bullet spawn point. This mostly happens when I click fire quickly and not when I hold the fire button down. I wanted a muzzle flash whenever I shot a bullet and for it to disappear quickly after. Here’s the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Weapon : MonoBehaviour
{
public Transform FirePt;
public GameObject bulletPrefab;
public GameObject mFlash;
public float invokeDelay = 0.1f;
public float fireDelay = 0.16f;
float fireElapsedTime = 0;
void Start()
{
mFlash.SetActive(false);
}
void Update()
{
fireElapsedTime += Time.deltaTime;
if (Input.GetButton(“Fire1”) && fireElapsedTime >= fireDelay)
{
mFlash.SetActive(true);
Invoke(“Shoot”, invokeDelay);
}
}
void Shoot()
{
Instantiate(bulletPrefab, FirePt.position, FirePt.rotation);
fireElapsedTime = 0;
mFlash.SetActive(false);
}
}