Time.deltaTime getting faster each time
In my project, I want to change the color of the background in the game with Color.Lerp function. In update I made a "if"and wrote this in it:
slowMode = true;
kamera.GetComponent<Camera>().backgroundColor = Color.Lerp(normal, slow, c);
c += Time.deltaTime;
This one is working fine at the start. It just takes 1 second to lerp. fine as I said.
But when I wanted to change back to the "normal" color from "slow" it takes lesser than 1 second it takes like 0.4 seconds. I am using this code this is supposed to get back to the normal color in 1 second.
kamera.GetComponent().backgroundColor = Color.Lerp(normal, kamera.GetComponent().backgroundColor, c); c -= Time.deltaTime;
How can I fix this?
Answer by TheMemorius · Oct 24, 2021 at 02:19 PM
In the first lerp between normal
and slow
, you got two unchanging values that you lerp in between. This produces the effect you want: Linear interpolation between two values. But when you move in the other direction, you're lerping between a fixed value (`normal`) and the current background color, which is a value that is changing each frame. So the range that you're moving across is shrinking while you're moving across it, which distorts the movement and gives you a curved rather than a linear movement.
(It should still take 1 second to reach the target value, but you'll reach a value very close to that target in a much shorter time, making it look like everything is just sped up.)
What you need is a lerp between two values that don't change - so don't use the changing background value as a parameter to the lerp function. The easiest fix would be to use your slow
variable instead:
kamera.GetComponent().backgroundColor = Color.Lerp(normal, slow, c);
c -= Time.deltaTime;
Thank you, I did it kamera.GetComponent().backgroundColor = Color.Lerp(normal, kamera.GetComponent().backgroundColor, c); c -= Time.deltaTime;
this way because there was another thing changing the background color. When both were used in a few seconds, background color was just going crazy a little bit, since it is a low possibility to these background changers get used together, in a few seconds... the other one is more important and more possible for a player to see.
I will use your solution.
Answer by EvilBob99 · Oct 26, 2021 at 09:17 AM
would it not be possible to put this in fixed update I'm not to sure on this because im still pretty bad at programming
Your answer
