I want to create a muzzle flash for my gun using a premade one from the assest store. Would I have to create a prefab of the muzzle flash and instantiate that prefab infront of muzzle or can I just make the muzzle flash just set it active when I press down on the mouse and deactivated it when im done shooting.? How would I create that muzzle flash effect in code? Thank you!
Yes, do that.
In code it’s literlly just flash.SetActive(true);
and flash.SetActive(false)
, assuming flash is a reference to your muzzle flash object.
So I added this to my code and when I shoot the muzzle flash just disappears so is there a way to have the muzzle flash always there in position infront of muzzle but only shows up when I shoot then turn off after im shooting? Could you show how to add it to my code I feel like this is so easy but im not doing something right.
shooting code:
void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (ammo > 0)
{
muzzlePrefab.SetActive(true);
ammo--;
GameObject bulletObject = ObjectPoolingManager.Instance.GetBullet(true);
bulletObject.transform.position = playerCamera.transform.position + playerCamera.transform.forward;
bulletObject.transform.forward = playerCamera.transform.forward;
muzzlePrefab.SetActive(false);
}
}
}
You are turning the muzzle prefab on and then off again in the same frame. Even if you could see at 60 frames/second, you still wouldn’t see this because the actual rendering step happens later, after all the Update methods have completed. So with the code above, the muzzle prefab is never active when any frame is rendered.
So you need to turn the flash on, and then much later, like after 1/3rd of a second or whatever, turn it off again. You can store the current Time.time value in a property on your class, and use that to know when enough time has elapsed to turn it back off.
I’d probably put a script on the muzzle flash itself, which turns itself off after a specific amount of time. Then in you main script here, you only ever worry about turning it on. Optionally you could reset the timer on the muzzle flash script if you want to turn the flash on, but it is already on, so it stays on longer.
You asked originally about instantiate/destroy the muzzle flash with each use. That can work, but is rather wasteful as far as performance. I’d only do it that way if you were somehow creating unique muzzle flashes each time (maybe you have 30 or so muzzle flash prefabs you randomly choose from).
I tried making the muzzle flash have its own script and just turn it on in the main script when I shoot ,but when i shoot it just stays turn on. I tried to make a timer and when that timer is greater than the waitTime that the muzzle flash would just turn off but thats not working
public class MuzzleFlash : MonoBehaviour
{
public GameObject mzFlash;
private float mzwaitTime = 1f;
private float mzTimer = 0.0f;
public void pistolFlash()
{
mzFlash.SetActive(true);
mzTimer += Time.deltaTime;
if (mzTimer > mzwaitTime)
{
mzFlash.SetActive(false);
}
}
}
Your MuzzleFlash script never updates mzTimer after it starts. Try this below, and only call pistolFlash() on the frame you want to activate a muzzle flash:
public class MuzzleFlash : MonoBehaviour
{
public GameObject mzFlash;
private float mzwaitTime = 1f;
private float mzTimer = 0.0f;
public void pistolFlash()
{
mzFlash.SetActive(true);
mzTimer = 0f;
}
void Update()
{
mzTimer += Time.deltaTime;
if (mzTimer > mzwaitTime)
{
mzFlash.SetActive(false);
}
}
}
This actually work thank you so much!