- Home /
 
Particle effect spawned lags behind parented object
I have a gun barrel and I spawn a particle effect at the end of it when the gun fires. Everything's OK when the gun isn't moving, but if the player does move, the effect is spawned, but is offset from the barrel.
I have the effect in the scene and I use Instantiate() to create a copy of it. I then set the transforms like so:
 effect.transform.parent = spawnPoint.transform;
 effect.transform.localPosition = Vector3.zero;
 
               Where 'spawnPoint' is the end of the barrel. I've stepped through the code and everything looks OK. This also effects anything else spawned from this point. E.g. bullets. They look like they're firing from outside the gun when the player is moving.
I have the same problem with a Particle System, the visual origin lags behind when the parent is rotating BUT additionally the visual origin stays at the same Y-offset to the parent, which makes absolutely no sense.
Particle Systems are obviously bugged since a couple of weeks.
Do you have a animated humanoid character as parent? Is the gun animated somehow? I believe it has to do with this, because I tested it with plain emtpy gameobjects and scripted the parent one to rotate, and the visual origin was dead on the gizmo!
Answer by Eudaimonium · Oct 15, 2015 at 06:28 PM
The spawnPoint obviously isn't correctly set, then. I assume this is an empty Game Object somewhere within your character hierarchy? Double check what it is parented to - it's very important.
Probably the parent of that game object is wrong and it needs to be moved to a node that controls the gun position (or something similar, depends on your setup).
To debug, simply place a small cube or whatever at the spawnPoint and see what happens in the game, how does it move. Re-parent it and try again.
It's parented correctly. I added gizmos to the end of the barrel and they follow lock-step with the player movement. It's only the effect that lags behind the action. I checked the code and it sets the correct position; the effect position is correctly set to the end of the barrel position.
Here's a screen grab of the code after a run through:

Perhaps the particle effects you're trying to spawn (muzzle flash I assume) is in World space, and therefore once spawned stays in the same place - is that the problem you're trying to solve? Basically what you need to do is go to the particle prefab you are using, and find the World Space / Local Space simulation switch, it's somewhere in the last tab "Rendering" I assume. This makes even the spawned particles follow your object ins$$anonymous$$d of just floating in the world.
The effects were in local space, changing to world space made it worse, as you suspected. Problem is not just with muzzle flash, I also spawn a projectile at the end of the barrel and assign it the end of the barrel position, but it lags behind too.
I managed to screen shot the effect. The gizmos are at the spawn point (effectively the end of the barrel, it's currently the middle of the gun, but whatever). This is the point that I'd expect the particle system to spawn at, what happens though is it spawns several units away.

Your answer