- Home /
How to set gun muzzle flash to end of gun with offset regardless of player's position/rotation?
I can create a muzzle flash at the end of my gun using Instantiate(muzzleFlash, gunBarrelTransform.transform.position, transform.rotation);
but I don't know how to offset it slightly so it's a little in front of the gun, because whatever I try this using something like Instantiate(muzzleFlash, gunBarrelTransform.transform.position + new Vector3(0.0f, 0.0f, 1.0f), transform.rotation);
it seems to create it at the spot I say so long as I don't turn the player but not relative to the direction the player is facing if I turn at all (as in, if I turn a bit from the player's default forward position then it creates the flash somewhere outside of where the camera is looking--I can turn quickly to see it there), which is obviously completely useless.
How do I correctly create the muzzle flash a little bit in front of the gun barrel (as in the barrel objects position plus a little bit forward), so it appears in the right place regardless of what direction I'm facing when it's created?
I really need to finally figure out how to correctly create stuff at a position plus a little bit and irrespective of whatever direction because this has been plaguing me for ages?
Answer by Der-Die-Das · Sep 14, 2018 at 04:45 AM
Just create a new empty Game Object and place it where you want the muzzle flash to be. Then use that transform as a reference instead of the gunBarrelTransform.
Answer by impurekind · Sep 14, 2018 at 11:45 AM
I already have an empty object attached as a child on each of the gun's barrels where I want the muzzle flash to appear. This doesn't give me any idea of the actual code I need to use in order to avoid the issues that are cropping up when I also try to give it a bit of an offset (which I'm clearly doing wrong)?