- Home /
How to make a float incrementally go up, reach the top, then go back down again and stop
I'm trying to get a fade out then in on the alpha. Can't seem to get it. Player will teleport, during the switch screen fades out then in. This is my code but it just fades out... I'm pretty new at this so detailed explanations are appreciated! I have a feeling it has something to do with Mathf.Sin?
void Update () {
panel.GetComponent<Image> ().color = new Color(0,0,0,alpha);
if (alphaTog <= 0) {
alpha += fadeSpeed * Time.deltaTime;
if (alpha == 255f) {
alphaTog = 1;
}
} else if(alphaTog == 1){
alpha -= fadeSpeed * Time.deltaTime;
}
Comment
alpha += fadeSpeed * Time.deltaTime;
if (alpha == 255f) {
First of all, color components go from 0.0 to 1.0, not from 0 to 255.
Secondly, you're increasing alpha in small increments, the chance that you hit the target value exactly, is very small. Use ">=" for the comparison.
Best Answer
Answer by gamecraftCZ · Dec 12, 2016 at 04:13 PM
Try this:
void Update () {
panel.GetComponent<Image> ().color = new Color(0,0,0,alpha);
if (alphaTog <= 0) {
alpha += fadeSpeed * Time.deltaTime;
if (alpha >= 1f) { //if alpha is 1 or more
alphaTog = 1;
alpha = 1f; //set alpha to 1
}
} else if(alphaTog == 1){
alpha -= fadeSpeed * Time.deltaTime;
if (alpha <= 0) { //if alpha is 0 or less
aplha = 0f; //set alpha to 0
alphaTog = 2; //This stop it.
}
}