0
0
Unityframework~10 mins

Color and size over lifetime in Unity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Color and size over lifetime
Start Particle
Initialize Color & Size
Update Lifetime
Calculate Color & Size based on Lifetime
Apply Color & Size
Check if Lifetime Ended?
NoLoop Update
Yes
Destroy Particle
This flow shows how a particle's color and size change as its lifetime progresses until it is destroyed.
Execution Sample
Unity
void Update()
{
  float lifeRatio = elapsedTime / totalLifetime;
  color = Color.Lerp(startColor, endColor, lifeRatio);
  size = Mathf.Lerp(startSize, endSize, lifeRatio);
}
This code updates the particle's color and size smoothly over its lifetime.
Execution Table
StepelapsedTime (s)lifeRatioColor (R,G,B,A)SizeAction
10.00.0(1,1,1,1)1.0Initialize particle with startColor and startSize
20.50.1(0.9,0.9,0.9,0.9)1.1Update color and size slightly
31.00.2(0.8,0.8,0.8,0.8)1.2Update color and size
42.50.5(0.5,0.5,0.5,0.5)1.5Mid lifetime color and size
54.00.8(0.2,0.2,0.2,0.2)1.8Near end color and size
65.01.0(0,0,0,0)2.0Lifetime ended, final color and size
75.01.0(0,0,0,0)2.0Destroy particle
💡 Particle lifetime reached 5.0 seconds, condition to destroy particle met.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
elapsedTime0.00.51.02.54.05.05.0
lifeRatio0.00.10.20.50.81.01.0
color(1,1,1,1)(0.9,0.9,0.9,0.9)(0.8,0.8,0.8,0.8)(0.5,0.5,0.5,0.5)(0.2,0.2,0.2,0.2)(0,0,0,0)(0,0,0,0)
size1.01.11.21.51.82.02.0
Key Moments - 3 Insights
Why does the color and size change smoothly instead of jumping suddenly?
Because the code uses interpolation (Lerp) based on lifeRatio, which gradually changes from 0 to 1 as elapsedTime increases (see execution_table rows 2 to 6).
What happens when elapsedTime equals totalLifetime?
The particle reaches the end of its lifetime, color and size reach their final values, and the particle is destroyed (see execution_table rows 6 and 7).
Why is lifeRatio calculated as elapsedTime divided by totalLifetime?
This normalizes elapsedTime to a 0-1 range, making it easy to interpolate color and size proportionally over the particle's lifetime (see variable_tracker lifeRatio values).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the color at step 4?
A(0.8,0.8,0.8,0.8)
B(0.2,0.2,0.2,0.2)
C(0.5,0.5,0.5,0.5)
D(1,1,1,1)
💡 Hint
Check the Color column at step 4 in the execution_table.
At which step does the size first reach 1.5?
AStep 4
BStep 3
CStep 5
DStep 6
💡 Hint
Look at the Size column in the execution_table and find when size is 1.5.
If totalLifetime was increased to 10 seconds, how would lifeRatio at step 4 change?
AIt would be 1.0
BIt would be 0.25
CIt would be 0.5
DIt would be 0.8
💡 Hint
lifeRatio = elapsedTime / totalLifetime; step 4 elapsedTime is 2.5 seconds.
Concept Snapshot
Color and size over lifetime in Unity:
- Use elapsedTime / totalLifetime to get lifeRatio (0 to 1).
- Use Color.Lerp(startColor, endColor, lifeRatio) to change color smoothly.
- Use Mathf.Lerp(startSize, endSize, lifeRatio) to change size smoothly.
- Update these every frame until lifetime ends.
- Destroy particle when lifetime is over.
Full Transcript
This visual execution shows how a particle's color and size change smoothly over its lifetime in Unity. The key is calculating lifeRatio as elapsedTime divided by totalLifetime, which goes from 0 to 1. Using this ratio, the code interpolates color and size from start to end values using Lerp functions. Each step updates elapsedTime, recalculates lifeRatio, and applies new color and size. When elapsedTime reaches totalLifetime, the particle is destroyed. This approach ensures smooth transitions and proper lifetime management.