Got it 👍 You want to trigger the fog color animation only when an event occurs, and handle the color change over time with a coroutine. That way, the fog will transition smoothly and not always run in Update().
Here’s an example:
using System.Collections;
public class FogColorAnimator : MonoBehaviour
public Color targetColor = Color.blue;
public float duration = 2f;
private Coroutine currentRoutine;
// Call this from an event
public void AnimateFogToColor(Color newColor, float time = -1f)
// Stop any existing animation
if (currentRoutine != null)
StopCoroutine(currentRoutine);
currentRoutine = StartCoroutine(AnimateFogColor(newColor, time > 0 ? time : duration));
private IEnumerator AnimateFogColor(Color newColor, float time)
Color startColor = RenderSettings.fogColor;
elapsed += Time.deltaTime;
float t = elapsed / time;
RenderSettings.fogColor = Color.Lerp(startColor, newColor, t);
yield return null; // wait for next frame
RenderSettings.fogColor = newColor;
- Attach this script to a
GameObject.
- When your event occurs (e.g. button press, trigger enter, custom game event), call:
// Example: from another script
FindObjectOfType<FogColorAnimator>().AnimateFogToColor(Color.red, 3f);
This will smoothly transition the fog color from its current value to red over 3 seconds.
👉 Do you want the fog to return back to the original color after some time (like a pulse effect), or just stay on the new color once the coroutine ends?