- Home /
Setting particle position every frame...
I'm trying to get my particles to layer nicely with my sprites as displayed here:
https://twitter.com/moaiHeadOlgo/status/732378209782779904
My problem is, I need to set the particles' z position every frame. When I do this, the particle looks like its z-fighting the sprite its supposed to be behind.
The reason for this, is so the torch flame can appear infront of / behind the player depending on what direction the player is facing.
I've tried doing "particleSys.Emit()" and passing in position parameters but then I lose all the nice functionality of the particle system prefabs.
Help! Thanks!
Answer by FortisVenaliter · May 20, 2016 at 02:43 PM
Do the particles have any movement predefined that might cause them to move a bit each frame from where you place them?
If not, are you placing them on the exact same z as the character? Or slightly ahead/behind?
Seeing your code would definitely help if neither of the previous two suggestions help.
Thanks for your reply!
There is no movement predefined other than Velocity over TIme = (0,1,0) in World space. No other script manipulates the position of the particles.
The particle system is a prefab that is placed on the torch slightly in front. I take the torch's position and add -.001 to the z value of the particle. Here's my code:
ParticleSystem.Particle[] particles;
ParticleSystem partSys;
void Update(){
int numAlive = partSys.GetParticles(particles);
for( int i = 0; i < particles.length; i++){
particles[i].position = new Vector3( particles[i].position.x, particles[i].position.y, transform.parent.position.z - .001f);
}
partSys.SetParticles(particles, numAlive);
}
Have you tried upping that offset? Try -0.1f or -1f and see what happens.
Yes, but the value needs to be small to fit between the torch at .001 and the head at -.001. Therefor, the value needs to be smaller than .002 to still be rendered behind the head.
I ran a test, I put the same code in a coroutine that runs once every second. The particles go to their proper position every second, but the very next frame they are back on top. I think this is a matter of the particles needing to be set every frame BEFORE they are drawn by the particle system.