- Home /
How to change fog density smoothly?
I might be doing stupid mistake, but I can't figure out where. My intention is to change the fog density to 0 over time. Using my code, the fog disappears instantly, but in editor while this method is called the value of fog density don't change at all. When I tried using extremely small "speed" game completely freezes.
void FogFading()
{
do
{
RenderSettings.fogDensity -= speed * Time.deltaTime;
} while (RenderSettings.fogDensity > 0f);
}
Answer by Iarus · Sep 01, 2017 at 05:58 PM
Your method will be executed all at once, meaning all in one frame. And like you described, the fog is all gone at the end of that frame. If you make speed really small, then that single frame will just take longer to compute.
What you want to do is to use a coroutine.
This is just pseudo code, you'll have to look at the documentation.
void FogFading()
{
StartCoroutine('FogFadingCoroutine');
}
IEnumerable FogFadingCoroutine()
{
do
{
RenderSettings.fogDensity -= speed * Time.deltaTime;
return yield null;
} while (RenderSettings.fogDensity > 0f);
}
Your answer
Follow this Question
Related Questions
Fog density over time. 1 Answer
No fog density settings in lighting and Global Fog image effect? 1 Answer
What is causing a single hiccup at start of IOS game? 1 Answer
Unity Editor Freezes Every Few Seconds 4 Answers
Android Player splash screen freezes when asking for microphone permission. 0 Answers