예제 #1
0
    void SpawnParticles(Vector3 pos)
    {
        int     spawnNumber = (int)(Volume * partsPerUnit);
        Vector3 spawnPos;
        List <ParticleSystem.Particle> newParticles = new List <ParticleSystem.Particle>();

        for (int i = 0; i < spawnNumber; i++)
        {
            spawnPos = pos + Random.insideUnitSphere * Radius / 2;

            if (gameArea.Contains(spawnPos))
            {
                particleContainer.Add(spawnPos);
                newParticles.Add(makeParticle(spawnPos));
            }
        }

        //Get the particles already in the system
        ParticleSystem.Particle[] particles_original = new ParticleSystem.Particle[particles.particleCount];
        particles.GetParticles(particles_original);

        ParticleSystem.Particle[] particles_new = newParticles.ToArray();

        //Combine the currently existing particles with the new ones you just created
        ParticleSystem.Particle[] particles_final = new ParticleSystem.Particle[particles_original.Length + particles_new.Length];
        particles_original.CopyTo(particles_final, 0);
        particles_new.CopyTo(particles_final, particles_original.Length);

        //And put them into the particle system
        particles.SetParticles(particles_final, particles_final.Length);
    }
        /// <summary>Called when a particle leaves the trigger.</summary>
        /// <author>Liam Ireland</author>
        private void OnParticleTrigger()
        {
            List <ParticleSystem.Particle> particles    = new List <ParticleSystem.Particle>();
            List <ParticleSystem.Particle> newParticles = new List <ParticleSystem.Particle>(); // an arraycontaining the particles to be added

            foreach (ParticleSystem.Particle particle in particles)                             // for every particle that just left a trigger
            {
                Collider closestCollider = closestStaticCollider(particle.position);
                Vector3  closestPointOnStaticCollider = closestCollider.bounds.ClosestPoint(particle.position);
                // get the distance from the particle to the shock wave origin
                float distanceFromParticleToOrigin = Vector3.Distance(particle.position, particleEmitter.transform.position);
                // get the distance from the closest point on the collider the particle nearlly missed to the shock wave origin
                float distanceFromColliderToOrigin = Vector3.Distance(closestPointOnStaticCollider, particleEmitter.transform.position);
                // if the distance from the particle to the shockwave origin is greater than the closest point to the particle on the collider we're checking to the shockwave origin - it makes sense in my mind, I don't know how to explain it easisly
                if (distanceFromParticleToOrigin > distanceFromColliderToOrigin)
                {
                    // doesn't quite work as intended yet - need to find a better check
                    // TODO - find a better check above
                    if (particle.velocity.y <= 0)   // pretty dumb, need more robust solution
                    {
                        for (int i = 0; i < 4; i++) // create multiple new particles for every near miss of a static collider
                        {
                            ParticleSystem.Particle newParticle = new ParticleSystem.Particle();
                            newParticle            = particle;
                            newParticle.velocity   = new Vector3(newParticle.velocity.x + UnityEngine.Random.Range(-1, 1), newParticle.velocity.y - (i * 0.75F), newParticle.velocity.z + UnityEngine.Random.Range(-1, 1));
                            newParticle.startColor = new Color32(255, 0, 0, 255);
                            newParticles.Add(newParticle);
                        }
                    }
                }
            }
            ParticleSystem.Particle[] oldParticles = new ParticleSystem.Particle[particleEmitter.particleCount];
            particleEmitter.GetParticles(oldParticles);
            ParticleSystem.Particle[] finalParticles = new ParticleSystem.Particle[oldParticles.Length + newParticles.Count];
            oldParticles.CopyTo(finalParticles, 0);
            newParticles.ToArray().CopyTo(finalParticles, oldParticles.Length);
            particleEmitter.SetParticles(finalParticles, finalParticles.Length);
        }