Problem with smooth fog color transition

Hi Community,

Could you please help me with my small issue. I have a small script, which purpose is to change fog colour over time. Right now it works, but there is no smooth transition between colours. Maybe someone encountered this situation.

(P.S. As you can see from script below, I am using Color.Lerp() to smoothly change fog colour)

using UnityEngine;
using System.Collections;

public class FogManager : MonoBehaviour {
	
	public float distanceFog = 500.0f;//Distance after which fog changes its color
	public float fogSmooth = 10.0f;//Smoother change value
	
	private Color currentFog;
	
	void Start() 
	{
		currentFog = RenderSettings.fogColor;//Storing current fog color value
		RenderSettings.fog = true;//Turning on in-game fog
	}
	
	void Update() 
	{
		
		if(PMove.distanceTraveled > distanceFog)
		{
			
			Debug.Log ("Fog color change");
			
			float fogRed = Random.Range(0.0f, 1.0f);
			float fogGreen = Random.Range(0.0f, 1.0f);
			float fogBlue = Random.Range(0.0f, 1.0f);
			
			currentFog = RenderSettings.fogColor;
			Color newFog = new Color(fogRed, fogGreen, fogBlue);
			
			Debug.Log ("Current Color:" + currentFog);
			
			Debug.Log ("Generated Color:" + newFog);
			
			RenderSettings.fogColor = Color.Lerp (currentFog, newFog, fogSmooth * Time.deltaTime);	
			
			distanceFog += distanceFog;
		}
	}
}

Your getting a new random color each frame, I would think your lerp is going to be going wild trying to hit a moving target and being given no time to get there. Maybe add the old colour to the new one before lerping so that any change is managable.

Well mccaber I do not think that you are right by saying that color gets generated every single frame. As you can see major part of this code is located inside the if statement, which executes only when statement hits true.

The problem is that color gets changed instantly, as if Lerp function is not working (at least not working the way expected). Maybe someone encountered same issue with Lerp()?

My thanks in advance.

why are you using deltaTime as the third parameter? all the examples for lerp on the reference use Time.time

edit: ok, I’ve had a bit of a play. The problem does seem to stem from the third parameter… if your fogSmooth * Time.deltaTime comes out over 1 the lerp will jump to the end. In the examples on the reference pages they use Time.time for the code to work for the first second, after that Time.time is greater than 1 so its the same issue.

In my really simple testing Time.deltaTime was coming out at 0.016 pretty consistently; so your code might not be zipping to the “end” but consistently returning the same value between 0 and 1?

Regardless, if you want the colours to change I think you’re going to need to store the start time of the transition and then do a “time since then * smoothing” as the third parameter (bare in mind that “smoothing” > 1 will speed up the transition).

Thanks LeftyRighty, will try it as soon as I get back home :slight_smile:

Btw I used Time.deltaTime instead of Time.time as first one is not dependent on frames. (not sure though do I need to put it in Update() or FixedUpdate())

Anyways will play at home and see the outcome. Thanks one more time.

hmm… ok, that logic works for things which build up frame by frame (ie how much further along am I?) but lerp doesn’t work quite like that.

Mathf.Lerp(0, 10, 0.5) => 5
Mathf.Lerp(0, 10, 2) => 10
Mathf.Lerp(0, 10, 10000000) => 10
Mathf.Lerp(0, 10, 0) => 0

if you pass deltaTime and your code runs consistently each frame you’ll be passing the same number into that third parameter each time. Maybe think of it as a “% how far along”.

have a look at the slerp function too, it works in a similar way but I think the code example gives a better idea of how they work:

By the way Lefty check this tutorial:

This guy uses Time.deltaTime in his situation and it works…

I have tried to output color values from Color.Lerp and it seems that it is lerping correctly, but with a crazy speed. Therefore I tried to pass small value (like 0.0016) to the fogSmooth variable, but it crashes my game. :face_with_spiral_eyes: So problem still exists…

Any ideas guys?