- Home /
C#: Particle Prefab doesn't follow it's parent (sort of)
I've created a gun and did all the usual stuff to it such as Raycast, sounds, recoil ect. Finally I've created the muzzleflash, which is a Prefab of an EmtyGameObject with a Ellipsoin Particle Emitter, a Particle Animator and a Particle Renderer. I loaded in my muzzleflash texture and it works just fine.
My GunScript references a GameObject called muzzleSpawn, which is an EmptyGameObject at the tip of the gun's barrel. This is where the MuzzleFlash Prefab gets Instantiated. In the same line of code I also declare the MuzzleFlash Prefab to be the child of the muzzleSpawn, therefore moving the Particles along with the gun as it rotates and jumps out of position by recoil and such.
In the Scene View I can see that the instantiated Prefab (the Muzzleflash) does indeed follow it's parent, however the actual particle (the visual aspect of it) itself does not move along with it. I have tried to parent the individual components(Particle Emitter, Renderer) of the Prefab directly to the bulletSpawn GameObject. The result is the same, I can not get it to work properly.
Here is a video of the problem: https://www.dropbox.com/s/bmi3746v8wskc60/Particle%20Parenting.mp4?dl=0
void Update ()
{
if (MuzzleFlash)
{
//Spawn Muzzleflash Particle & set Parent as muzzleSpawn
(Instantiate(MuzzleFlash, muzzleSpawn.transform.position, muzzleSpawn.transform.rotation) as GameObject).transform.parent = muzzleSpawn.transform;
/* MuzzleFlash.GetComponent<ParticleRenderer>().transform.parent = muzzleSpawn.transform;
MuzzleFlash.GetComponent<ParticleAnimator>().transform.parent = muzzleSpawn.transform;
MuzzleFlash.GetComponent<ParticleEmitter>().transform.parent = muzzleSpawn.transform;
*/
}
}
Solution:
I have found the answer. I simply needed to uncheck "Simulate in Worldspace?" in the Particle Emitter settings. The devil is in the details. You might want to leave the question up in case someone else runs into the same issue.
Your answer
Follow this Question
Related Questions
First child of a gameobject 3 Answers
Particle System wont play? 1 Answer
Parent colliding bullet to the object 1 Answer
How to parent these multiple objects to othe multiple objects? 2 Answers
Distribute terrain in zones 3 Answers