To create Explosions in my Unity Game, I pool 3 runtime-configured particle systems, with the stop-action equals to disable (so I can pool all disables particleObjects);
I do something like this:
public void CreateExplosions()
{
Pool_Particle_Simple(position, rotation, 8);
Pool_Particle_Simple(position, rotation, 9);
Pool_Particle_Simple(position, rotation, 10);
}
public PoolableParticle GetAvailabe_PoolParticle(PoolableParticle[] particles, int start, int end)
{
for (int i = start; i < end; i++)
{
PoolableParticle particle = particles[i];
switch (particle.gameObject.activeInHierarchy)
{
case false:
particles[i].gameObject.SetActive(true);
return particles[i];
}
}
return null;
}
public void Pool_Particle_Simple(Vector3 position, Quaternion rotation, ushort type)
{
PoolableParticle particle = GetAvailabe_PoolParticle(poolableParticles, 0, poolableParticles.Length);
switch (ReferenceEquals(particle, null))
{
case false:
ParticleSystem particleSystem = particle.ParticleSystem;
SetupParticle(particleSystem, particle.ParticleSystemRenderer, type);
particleSystem.transform.position = position;
particle.transform.rotation = rotation;
particleSystem.Play();
break;
}
}
public void SetupParticle(ParticleSystem particleSystem, ParticleSystemRenderer renderer, int type)//0 = Smoke01, 1 = Smoke02, ...
{
switch (type)
{
case 0://Smoke01
ParticleSystem.MainModule main = particleSystem.main;
main.duration = 5;
main.loop = false;
main.prewarm = true;
main.startDelay = 0;
main.startLifetime = new ParticleSystem.MinMaxCurve(4, 6);
main.startSpeed = new ParticleSystem.MinMaxCurve(.5f, 1f);
main.startSize3D = false;
main.startSize = 55;
main.startRotation = 0;
main.flipRotation = 1;
main.startColor = Color.white;
main.gravityModifier = 0;
main.simulationSpace = ParticleSystemSimulationSpace.World;
main.simulationSpeed = 1;
main.scalingMode = ParticleSystemScalingMode.Local;
main.emitterVelocityMode = ParticleSystemEmitterVelocityMode.Rigidbody;
main.maxParticles = 15;
main.cullingMode = ParticleSystemCullingMode.AlwaysSimulate;
main.ringBufferMode = ParticleSystemRingBufferMode.Disabled;
ParticleSystem.EmissionModule emission = particleSystem.emission;
emission.rateOverTime = 3;
emission.rateOverDistance = 0;
emission.burstCount = 0;
ParticleSystem.VelocityOverLifetimeModule velocity = particleSystem.velocityOverLifetime;
velocity.enabled = false;
ParticleSystem.LimitVelocityOverLifetimeModule limitVelocity = particleSystem.limitVelocityOverLifetime;
limitVelocity.enabled = false;
ParticleSystem.ShapeModule shape = particleSystem.shape;
shape.enabled = true;
shape.shapeType = ParticleSystemShapeType.Cone;
shape.angle = 25;
shape.radius = 0.3822865f;
shape.radiusThickness = 1;
shape.arc = 360;
shape.arcMode = ParticleSystemShapeMultiModeValue.Random;
shape.arcSpread = 0;
shape.length = 5;
shape.position = Vector3.zero;
shape.rotation = Vector3.zero;
shape.scale = Vector3.one;
shape.alignToDirection = false;
shape.randomDirectionAmount = 1;
shape.sphericalDirectionAmount = 0;
shape.randomPositionAmount = 0;
ParticleSystem.ForceOverLifetimeModule force = particleSystem.forceOverLifetime;
force.enabled = false;
ParticleSystem.ColorOverLifetimeModule color = particleSystem.colorOverLifetime;
color.enabled = true;
Gradient gradient = new Gradient();
gradient.mode = GradientMode.Blend;
GradientColorKey[] colorKeys = new GradientColorKey[2];
colorKeys[0] = new GradientColorKey(Color.white, 0f);
colorKeys[1] = new GradientColorKey(Color.white, 1f);
GradientAlphaKey[] alphaKeys = new GradientAlphaKey[4];
alphaKeys[0] = new GradientAlphaKey(0.12f, 0);
alphaKeys[1] = new GradientAlphaKey(.45f, 0.27f);
alphaKeys[2] = new GradientAlphaKey(.70f, .805f);
alphaKeys[3] = new GradientAlphaKey(0f, 1f);
gradient.SetKeys(colorKeys, alphaKeys);
color.color = gradient;
ParticleSystem.SizeOverLifetimeModule size = particleSystem.sizeOverLifetime;
size.separateAxes = false;
size.size = new ParticleSystem.MinMaxCurve(0, 1);
ParticleSystem.RotationOverLifetimeModule rotation = particleSystem.rotationOverLifetime;
rotation.enabled = true;
rotation.separateAxes = false;
rotation.z = .45f;
ParticleSystem.SubEmittersModule subEmitters = particleSystem.subEmitters;
subEmitters.enabled = false;
ParticleSystem.NoiseModule noise = particleSystem.noise;
noise.enabled = false;
ParticleSystem.TrailModule trails = particleSystem.trails;
trails.enabled = false;
renderer.renderMode = ParticleSystemRenderMode.Mesh;
renderer.meshDistribution = ParticleSystemMeshDistribution.UniformRandom;
renderer.sortMode = ParticleSystemSortMode.None;
renderer.sortingFudge = 0;
renderer.alignment = ParticleSystemRenderSpace.Local;
renderer.flip = Vector3.zero;
renderer.enableGPUInstancing = false;
renderer.pivot = Vector3.zero;
renderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
renderer.sortingLayerID = 0;
renderer.lightProbeUsage = UnityEngine.Rendering.LightProbeUsage.BlendProbes;
renderer.probeAnchor = null;
break;
case 8://Small Explosion (base)
main = particleSystem.main;
main.duration = 2f;
main.loop = false;
main.prewarm = false;
main.startDelay = 0f;
main.startLifetime = new ParticleSystem.MinMaxCurve(.75f, 1f);
main.startSpeed = 0;
main.startSize3D = false;
main.startSize = new ParticleSystem.MinMaxCurve(1f, 2f);
main.startRotation = new ParticleSystem.MinMaxCurve(0, 360);
main.flipRotation = 0;
main.startColor = Color.white;
main.gravityModifier = 0;
main.simulationSpace = ParticleSystemSimulationSpace.Local;
main.simulationSpeed = 1;
main.scalingMode = ParticleSystemScalingMode.Local;
main.emitterVelocityMode = ParticleSystemEmitterVelocityMode.Transform;
main.maxParticles = 1000;
main.cullingMode = ParticleSystemCullingMode.AlwaysSimulate;
main.ringBufferMode = ParticleSystemRingBufferMode.Disabled;
emission = particleSystem.emission;
emission.rateOverTime = 0;
emission.rateOverDistance = 0;
ParticleSystem.Burst[] bursts = new ParticleSystem.Burst[1];
bursts[0] = new ParticleSystem.Burst(0f, (short)5, (short)5, 1, 0.050f);
emission.SetBursts(bursts);
shape = particleSystem.shape;
shape.enabled = true;
shape.shapeType = ParticleSystemShapeType.Sphere;
shape.radius = 0.2442369f;
shape.radiusThickness = 1;
shape.arc = 360;
shape.arcMode = ParticleSystemShapeMultiModeValue.Random;
shape.arcSpread = 0;
shape.position = Vector3.zero;
shape.rotation = Vector3.zero;
shape.scale = Vector3.one;
shape.alignToDirection = false;
shape.randomDirectionAmount = 0;
shape.randomPositionAmount = 0;
shape.sphericalDirectionAmount = 0;
velocity = particleSystem.velocityOverLifetime;
velocity.space = ParticleSystemSimulationSpace.Local;
velocity.enabled = true;
velocity.xMultiplier = 0;
velocity.orbitalX = 0;
velocity.orbitalOffsetX = 0;
velocity.yMultiplier = 0;
velocity.orbitalY = 0;
velocity.orbitalOffsetY = 0;
velocity.zMultiplier = 0;
velocity.orbitalZ = 0;
velocity.orbitalOffsetZ = 0;
velocity.radial = new ParticleSystem.MinMaxCurve(0f, 2f);
velocity.speedModifier = 1;
force = particleSystem.forceOverLifetime;
force.enabled = false;
limitVelocity = particleSystem.limitVelocityOverLifetime;
limitVelocity.enabled = false;
color = particleSystem.colorOverLifetime;
color.enabled = true;
gradient = new Gradient();
gradient.mode = GradientMode.Blend;
colorKeys = new GradientColorKey[3];
colorKeys[0] = new GradientColorKey(Color.white, .019f);
colorKeys[1] = new GradientColorKey(Color.black, .036f);
colorKeys[2] = new GradientColorKey(Color.black, .694f);
alphaKeys = new GradientAlphaKey[3];
alphaKeys[0] = new GradientAlphaKey(1f, 0);
alphaKeys[1] = new GradientAlphaKey(1f, 0.688f);
alphaKeys[2] = new GradientAlphaKey(0f, 1f);
gradient.SetKeys(colorKeys, alphaKeys);
color.color = gradient;
size = particleSystem.sizeOverLifetime;
size.separateAxes = false;
size.size = new ParticleSystem.MinMaxCurve(1, 2);
rotation = particleSystem.rotationOverLifetime;
rotation.enabled = false;
subEmitters = particleSystem.subEmitters;
subEmitters.enabled = false;
noise = particleSystem.noise;
noise.enabled = false;
trails = particleSystem.trails;
trails.enabled = false;
renderer.renderMode = ParticleSystemRenderMode.Billboard;
renderer.normalDirection = 1;
renderer.sortMode = ParticleSystemSortMode.Distance;
renderer.sortingFudge = 0;
renderer.minParticleSize = 0;
renderer.maxParticleSize = 5f;
renderer.alignment = ParticleSystemRenderSpace.View;
renderer.flip = new Vector3(.5f, .5f, 0);
renderer.allowRoll = true;
renderer.pivot = Vector3.zero;
renderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
renderer.shadowBias = 0;
renderer.sortingLayerID = 0;
renderer.sortingOrder = 0;
renderer.lightProbeUsage = UnityEngine.Rendering.LightProbeUsage.Off;
break;
case 9://Small Explosion (additional smoke)
main = particleSystem.main;
main.duration = 2f;
main.loop = false;
main.prewarm = false;
main.startDelay = 0f;
main.startLifetime = new ParticleSystem.MinMaxCurve(1.5f, 1.2f);
main.startSpeed = 0;
main.startSize3D = false;
main.startSize = new ParticleSystem.MinMaxCurve(1.5f, 1f);
main.startRotation = new ParticleSystem.MinMaxCurve(0, 360);
main.flipRotation = 0;
main.startColor = Color.white;
main.gravityModifier = 0;
main.simulationSpace = ParticleSystemSimulationSpace.Local;
main.simulationSpeed = 1;
main.scalingMode = ParticleSystemScalingMode.Local;
main.emitterVelocityMode = ParticleSystemEmitterVelocityMode.Transform;
main.maxParticles = 1000;
main.cullingMode = ParticleSystemCullingMode.AlwaysSimulate;
main.ringBufferMode = ParticleSystemRingBufferMode.Disabled;
emission = particleSystem.emission;
emission.rateOverTime = 0;
emission.rateOverDistance = 0;
bursts = new ParticleSystem.Burst[1];
bursts[0] = new ParticleSystem.Burst(0f, (short)10, (short)10, 1, 0.100f);
emission.SetBursts(bursts);
shape = particleSystem.shape;
shape.enabled = true;
shape.shapeType = ParticleSystemShapeType.Sphere;
shape.radius = 0.3957672f;
shape.radiusThickness = 0;
shape.arc = 360;
shape.arcMode = ParticleSystemShapeMultiModeValue.Random;
shape.arcSpread = 0;
shape.position = Vector3.zero;
shape.rotation = Vector3.zero;
shape.scale = Vector3.one;
shape.alignToDirection = false;
shape.randomDirectionAmount = 0;
shape.randomPositionAmount = 0;
shape.sphericalDirectionAmount = 0;
velocity = particleSystem.velocityOverLifetime;
velocity.enabled = true;
velocity.xMultiplier = 0;
velocity.orbitalX = 0;
velocity.orbitalOffsetX = 0;
velocity.yMultiplier = 2;
velocity.orbitalY = 0;
velocity.orbitalOffsetY = 0;
velocity.zMultiplier = 0;
velocity.orbitalZ = 0;
velocity.orbitalOffsetZ = 0;
velocity.radial = new ParticleSystem.MinMaxCurve(0f, 1f);
velocity.speedModifier = 1;
force = particleSystem.forceOverLifetime;
force.enabled = false;
limitVelocity = particleSystem.limitVelocityOverLifetime;
limitVelocity.enabled = false;
color = particleSystem.colorOverLifetime;
color.enabled = true;
gradient = new Gradient();
gradient.mode = GradientMode.Blend;
colorKeys = new GradientColorKey[2];
colorKeys[0] = new GradientColorKey(new Color(255, 107, 1), 0f);
colorKeys[1] = new GradientColorKey(new Color(41, 41, 41), .341f);
alphaKeys = new GradientAlphaKey[4];
alphaKeys[0] = new GradientAlphaKey(0f, 0.01f);
alphaKeys[1] = new GradientAlphaKey(.5f, 0.247f);
alphaKeys[2] = new GradientAlphaKey(1f, .618f);
alphaKeys[3] = new GradientAlphaKey(0f, 1f);
gradient.SetKeys(colorKeys, alphaKeys);
color.color = gradient;
size = particleSystem.sizeOverLifetime;
size.separateAxes = false;
size.size = new ParticleSystem.MinMaxCurve(1, 2);
rotation = particleSystem.rotationOverLifetime;
rotation.enabled = false;
subEmitters = particleSystem.subEmitters;
subEmitters.enabled = false;
noise = particleSystem.noise;
noise.enabled = false;
trails = particleSystem.trails;
trails.enabled = false;
renderer.renderMode = ParticleSystemRenderMode.Billboard;
renderer.normalDirection = 1;
renderer.sortMode = ParticleSystemSortMode.Distance;
renderer.sortingFudge = 0;
renderer.minParticleSize = 0;
renderer.maxParticleSize = 5f;
renderer.alignment = ParticleSystemRenderSpace.View;
renderer.flip = new Vector3(.5f, .5f, 0);
renderer.allowRoll = true;
renderer.pivot = Vector3.zero;
renderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
renderer.shadowBias = 0;
renderer.sortingLayerID = 0;
renderer.sortingOrder = 1;
renderer.lightProbeUsage = UnityEngine.Rendering.LightProbeUsage.Off;
break;
case 10://Small Explosion (shockWave)
main = particleSystem.main;
main.duration = 2f;
main.loop = false;
main.prewarm = false;
main.startDelay = 0f;
main.startLifetime = .5f;
main.startSpeed = 0;
main.startSize3D = false;
main.startSize = 10;
main.startRotation = 0;
main.flipRotation = 0;
main.startColor = Color.white;
main.gravityModifier = 0;
main.simulationSpace = ParticleSystemSimulationSpace.Local;
main.simulationSpeed = 1;
main.scalingMode = ParticleSystemScalingMode.Local;
main.emitterVelocityMode = ParticleSystemEmitterVelocityMode.Transform;
main.maxParticles = 1000;
main.cullingMode = ParticleSystemCullingMode.AlwaysSimulate;
main.ringBufferMode = ParticleSystemRingBufferMode.Disabled;
emission = particleSystem.emission;
emission.rateOverTime = 0;
emission.rateOverDistance = 0;
bursts = new ParticleSystem.Burst[1];
bursts[0] = new ParticleSystem.Burst(0f, (short)1, (short)1, 1, 0.010f);
emission.SetBursts(bursts);
shape = particleSystem.shape;
shape.enabled = false;
velocity = particleSystem.velocityOverLifetime;
velocity.enabled = false;
force = particleSystem.forceOverLifetime;
force.enabled = false;
limitVelocity = particleSystem.limitVelocityOverLifetime;
limitVelocity.enabled = false;
color = particleSystem.colorOverLifetime;
color.enabled = true;
gradient = new Gradient();
gradient.mode = GradientMode.Blend;
colorKeys = new GradientColorKey[2];
colorKeys[0] = new GradientColorKey(Color.white, 0f);
colorKeys[1] = new GradientColorKey(Color.white, 1f);
alphaKeys = new GradientAlphaKey[2];
alphaKeys[0] = new GradientAlphaKey(1f, 0f);
alphaKeys[1] = new GradientAlphaKey(0f, 1f);
gradient.SetKeys(colorKeys, alphaKeys);
color.color = gradient;
size = particleSystem.sizeOverLifetime;
size.separateAxes = false;
size.size = new ParticleSystem.MinMaxCurve(0, 1);//Forse no
rotation = particleSystem.rotationOverLifetime;
rotation.enabled = false;
subEmitters = particleSystem.subEmitters;
subEmitters.enabled = false;
noise = particleSystem.noise;
noise.enabled = false;
trails = particleSystem.trails;
trails.enabled = false;
renderer.renderMode = ParticleSystemRenderMode.Billboard;
renderer.normalDirection = 1;
renderer.sortMode = ParticleSystemSortMode.None;
renderer.sortingFudge = 0;
renderer.minParticleSize = 0;
renderer.maxParticleSize = 20f;
renderer.alignment = ParticleSystemRenderSpace.View;
renderer.flip = Vector3.zero;
renderer.allowRoll = true;
renderer.pivot = Vector3.zero;
renderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
renderer.shadowBias = 0;
renderer.sortingLayerID = 0;
renderer.sortingOrder = 1;
renderer.lightProbeUsage = UnityEngine.Rendering.LightProbeUsage.Off;
break;
//.....
}
The problem is that some times I obtain artifacts like the one on the right side of the image enter image description here. instead, I would like to have a result like the left side one.
By Looking on the inspector, the two corrispective particles molules looks like equals, but as you can see, the result is very different (the artifacted one is very biggest, and has wrong colors and wrong shapes). What could cause that glitch?