Course contentsShow
← Unity Game Development
Lesson 660 of 82810. VFX and Post-ProcessingFree lesson

Building Dynamic Color Palettes with Lerp

Use linear interpolation nodes to create smooth color transitions and dynamic tinting effects that react to game state variables.

Building Dynamic Color Palettes with Lerp

In this lesson, you will learn how to use linear interpolation to transition seamlessly between colors based on real-time game state variables.

The Logic of Interpolation

While applying static gradients in a Shuriken Particle System is straightforward, advanced visuals often require colors that shift dynamically based on player health, time of day, or proximity to environmental hazards. The Lerp (Linear Interpolation) function is the foundation of this behavior. Mathematically, Lerp(Color A, Color B, t) calculates a new color by sliding a value t (from 0 to 1) between your starting and ending colors.

Practical Implementation in Shader Graph

To drive these transitions within a shader, you can expose a property like HealthPercentage (a float between 0 and 1) to act as your t value. Inside the Shader Graph, you would pipe this variable into the T input of a Lerp node, with your "Healthy" color in the A input and your "Critical" color in the B input.

When your script updates the HealthPercentage material property, the shader automatically recalculates the pixel color:

// Example of driving a material property via script
void Update() {
    float healthNormalized = currentHealth / maxHealth;
    // Update the shader property dynamically
    myRenderer.material.SetFloat("_HealthPercentage", healthNormalized);
}

By connecting this Lerp output to the Base Color or Emission property, your object’s tint will shift fluidly. If you need more complex palettes, you can nest multiple Lerp nodes to transition through three or four colors, essentially building a dynamic gradient that reacts to your game's logic. This approach is far more performant than swapping textures, as it relies on simple mathematical operations calculated directly on the GPU.

By utilizing this technique, you can create visual feedback loops—such as an engine glowing hotter as it approaches a red-line threshold—without the overhead of heavy asset swaps.

Key Takeaway: By using Lerp to map game state variables to color inputs, you create highly reactive, performance-efficient visual transitions that respond fluidly to player actions.