![]() ParticleSys.SetParticles(particles, particles.Length) // write those pesky particles back into our ParticleSystem If (lastParticleIndex >= maxParticles) lastParticleIndex = 0 LastParticleIndex++ // keep track of oldest particle we need to reset start lifetime to a normal value, too or the particle will still have infinite lifetime set lifetime to -1 to kill the particle / particle system with ParticleSystem.SetParticles( particles, count ) / and then when we're done, write the particles back into the After that, we can directly modify the particles / write out the existing particles in the particle system to our / basically the parameter (particles) is an out parameter which will Read the Unity docs VERY carefully to understand. Int activeParticles = particleSys.GetParticles(particles) if we're at our particle count limit, kill our oldest particle. Public void CreateParticle(Vector3 position, float size, Vector3 velocity, float angularVelocity) Initialize() // initialize all of our member variables Private int maxParticles = 0 // total number of particles in our scene before re-using Private ParticleSystem.EmitParams emitParams // reusable emitparams Private ParticleSystem.Particle particles // our reusable array of particles Private ParticleSystem particleSys // our object's particle system these will all be inited in Initialize() on Start() Private int lastParticleIndex = 0 // keeps track of our oldest particle (for deletion) Public class ParticlePool : MonoBehaviour Note that this script was written for Unity 2018.x, but still works great as of 2020.2.1Īrtifact-Jesse's complete, functional example of a fairly generic particle pool: using UnityEngine I'll include the relevant script here for reference (complete with colorful commentary for clarity). I think it achieves exactly what you're looking for. It's a particle pool from which you can emit one or more particles as needed, killing off the last particle made and reusing it once it reaches its max number of particles. I needed to do something similar and found this answer in the Unity forums gave me a great, reliable solution that I've reused and modified to my purposes many times. Var emitParams = new ParticleSystem.EmitParams() ĮmitParams.position = new Vector3(0.0f, 0.0f, 0.0f) ĮmitParams.velocity = new Vector3(0.0f, 0.0f, 0.0f) ĪSystem.Play() // Continue normal emissionsĮxpected result: A stream of particles moving in the +z direction.Īctual result: One particle moving in the +z direction. ParticleSystemRenderer aRenderer = aSystem.GetComponent() ĪRenderer.renderMode = ParticleSystemRenderMode.Mesh Īsh = Resources.Load("vector") Configure render settings for particle. Int numParticles = aSystem.GetParticles(aParticle) ĭebug.Log("Number of particles: " + numParticles) ĪParticle.position = new Vector3(0f,0f,0f) ĪParticle.velocity = new Vector3(0f, 0f, 1f) Private ParticleSystem.Particle aParticle ĪParticle = new ParticleSystem.Particle ĭebug.Log("Particle Count: " + aSystem.particleCount) I'm able to check and alter particle position in Update(), so I don't think I'm having some kind of scope problem. Using a separate method called using Invoke() in the update loop.Using a separate method called directly in the update loop.Using Emit() directly in the update loop.When trying to emit particles in the update methods I have tried: I reported the number of particles in the update methods to make sure that the code there was actually running, and it reports however many particles I emit in the Start() method. When attempting to do the same in Update(), LateUpdate(), or FixedUpdate() it no longer works. I am able to emit particles in the Start() method, either directly or with a separate method. I am trying to use ParticleSystem.Emit() to dynamically emit particles, and am unable to get Emit() to work outside of the Start() method.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |