Survival shooter text fade in
Hello I recently finished Survival shooter tutorial and I came across some problem. I want my game over text to fade in after i die so i wrote this into my update function Color targetColor = new Color(255, 0, 0, 255);
if(isDead)
{
gameOver.color = Color.Lerp(gameOver.color, targetColor, 5f * Time.deltaTime);
}
the output of this is that there is no linear transition between color with no alpha and targetColor with alpha 255. What am I missing ? What is solution to make gameOver text slowly appear for 5 second after isDead condition is true ? Thank you all for help
Answer by hexagonius · Dec 02, 2016 at 02:46 PM
The last parameter of the Lerp works like a slider with 0 choosing the first parameter 100% and 1 the second 100%. If deltaTime was constant like fixedDeltaTime is and it nearly is, you're basically passing in a constant and the resulting alpha is the same value every frame. What you need to do is have a float variable somewhere with a value of 0, increasing it every frame. So in your if statement you would put something like
yourFloat += Time.deltaTime / 5f;
this will increase yourFloat by a 5th of the passed time every frame essentially setting it to 1 and larger after 5 seconds.
Looks reasonable, but still doesnt work for me. I've tried to put in one more if statement to check if my float reach 5f, but it still fade in(respect. change color in this code) immediately after the isDead condition is made
if(isDead) alphaUp += Time.deltaTime/5f; if(alphaUp >= 5f) { gameOver.color = new Color(255, 0, 0, 255); } //gameOver.color = Color.Lerp(gameOver.color, targetColor, alphaUp);
Oh sorry that was mistake from my side ( and it was really dumb one ) I forgot to comment on line of code that cause to immediately change the alpha. It was outside this block of code. Really thank you for your help!