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

Building Screen Flash Effects for Damage Feedback

You will implement a screen-space overlay shader that flashes to provide immediate visual cues when the player takes damage.

In this lesson, you will learn how to create a high-impact screen flash effect using a dedicated overlay shader to provide immediate, intuitive visual feedback when the player takes damage.

The Concept: Screen-Space Overlays

Rather than relying on UI elements that sit "above" the game, we use a screen-space overlay shader. By manipulating the alpha channel of a full-screen quad (or a custom Render Feature), we can modulate the screen color instantly upon damage. The core logic involves passing a "Flash Intensity" parameter from your damage script to the shader, allowing the screen to pulse from transparent to a saturated color (like red or white) and back to opaque over a short duration.

Implementing the Shader logic

The shader itself is straightforward; it treats the screen texture as a background layer and performs an additive blend with your flash color based on a scalar property.

// Simplified DamageFlash.shader snippet
float4 frag(v2f i) : SV_Target {
    float4 sceneCol = tex2D(_MainTex, i.uv);
    // Linearly interpolate between the scene and the flash color
    float4 flashCol = lerp(sceneCol, _FlashColor, _FlashIntensity);
    return flashCol;
}

Orchestrating via Scripting

To make this reactive, your PlayerHealth script should manage a timer that controls the _FlashIntensity property of the Material. When TakeDamage() is triggered, you set the intensity to a high value (e.g., 0.8) and use Mathf.MoveTowards or a Coroutine to decay that value back to zero.

Because we are dealing with high-frequency visual feedback, ensure you are using MaterialPropertyBlock if you are applying this to a screen-space quad. This avoids creating material instances per-frame, keeping your draw calls optimized—a critical practice from our previous performance optimization lessons. By decoupling the flash duration from the gameplay logic, you ensure the feedback remains snappy and responsive regardless of frame rate fluctuations.

Key Takeaway: Use a shader-based overlay controlled by a decay timer to create persistent, performance-efficient visual feedback that informs the player of damage without interrupting the game's depth or atmosphere.