- Home /
How to change already emitted particles color with gradient in a sequential formation from center to outward direction on key press?
Hi,
I have a query regarding changing gradient color of already emitted particles. Starting color of the particle system is 40F8E9FF. I have used cone shape to achieve this form in which emission is done from Base.
I have 3 gradient colors which are predefined as shown in the photo below:
1. 40F8E9 to 8264FF
2. FF64EB to D8EC4A
3. 64FF78 to FF770F
Question: 1. How can I choose a color from the gradient mentioned in the presets. 2. How can I write a code, in which if I press a key, it pickups the color from preset, and creates an animate of color change of all particles from center to outward in a sequential progressive manner.
Till now I could use a script which helped me to change the color of all the emitted particles immediately but it is not animating.
I also tried to change m_Particles[ i +1 ].startColor, m_Particles[ i+2 ].startColor to gradiant color but my application crashed.
Please help me to solve this.
[RequireComponent(typeof(ParticleSystem))]
public class ChangeAllParticleColor : MonoBehaviour {
ParticleSystem m_System;
ParticleSystem.Particle[] m_Particles;
public float m_Drift = 0.01f;
private Color32 color;
private void LateUpdate()
{
InitializeIfNeeded();
// GetParticles is allocation free because we reuse the m_Particles buffer between updates
int numParticlesAlive = m_System.GetParticles(m_Particles);
// Change only the particles that are alive
for (int i = 0; i < numParticlesAlive; i++)
{
//m_Particles[i].velocity += Vector3.up * m_Drift;
color = new Color(1, 1, 1);
m_Particles[i].startColor = color;
}
// Apply the particle changes to the particle system
m_System.SetParticles(m_Particles, numParticlesAlive);
}
void InitializeIfNeeded()
{
if (m_System == null)
m_System = GetComponent<ParticleSystem>();
if (m_Particles == null || m_Particles.Length < m_System.main.maxParticles)
m_Particles = new ParticleSystem.Particle[m_System.main.maxParticles];
}
}
I am a designer but trying to write a script for animation.
Regards, DK
Answer by richardkettlewell · Sep 12, 2018 at 08:32 PM
Set the gradient on the ColorOverLifetime module.
Your answer
Follow this Question
Related Questions
Can I make animations snap to a frame? 1 Answer
How active my particle system made in blender in unity? 1 Answer
Have particles follow animated character 1 Answer
Prevent particles from passing through each other 0 Answers
Animating a gradient? 0 Answers