CanvasGroup thisCanvas;
void Awake () {
thisCanvas = GameObject.Find("Canvas").GetComponent<CanvasGroup> ();
}
void Start () {
StartCoroutine(Cutscene());
}
public IEnumerator Cutscene () {
thisCanvas.alpha = Mathf.Lerp(0f, 1f, 0.08f);//I think you missunderstood how Lerp works.
yield return new WaitForSeconds (2f); //Waits 2 seconds then goes to the next line, which brings it back to full brightness. So the light variation is never more than 8%.
thisCanvas.alpha = Mathf.Lerp(1f, 0f, 0.08f); //Again
}
Lerp is a solid interpolation point between 2 points. It defaults to halfway but you can change it. That is how you use it to smooth, you change the last paramater (the timer) at a fprogrammed rate. rate (most common can get advanced if ya like)
Lerp is this, StartPoint, EndPoint, A value clamped between the start and end point (so basically like a joystick axis). However once you set the axis that is where it is until you move it. It has great usage by just manipulating it to intuitively move between max and min.
So Your coroutine starts. You basically just said thisCanvas.alpha = 0.08f;
Wait 2 seconds.
thisCanvas.alpha = 1f;
A good way to move the t value is to do like
t = 0; //So it will start at off.
Mathf.Lerp(0f,1f,t += 0.08);
t is clamped so even if you overrun it, if you base your light intensity off of t based on (Mathf.Lerp(a,b,t) it can never get higher than b or lower than a.
With that everytime the game loop passes the lerp line it moves further towareds 1.0f at an increment of 0.08f If you use Time.Deltatime as the adder it will add the amount of time since the last fixed update (1 second unless you change it) to t; That makes a nice smooth transition.
So If what you are wanting to do is make the light smoothly fade from off to full on back to full off over and over it might look something like this.
Meh others tried to answer so here ya go. I am a bit out of it so hope I didnt make too many typos, should be close.
public IEnumerator CutsceneAmplify () {
if(t != 0f){ t = 0;);
thisCanvas.alpha = Mathf.Lerp(0f, 1f, t +=0.08f);
if(t < 1f) {
yield return new WaitForSeconds (2f);
}
else {
yield return StartCoroutine(CutscenDiminish());
}
}
public IEnumerator CutsceneDiminish () {
if(t != 1f){ t = 1f;);
thisCanvas.alpha = Mathf.Lerp(0f, 1f, t -= 0.08f);
if(t < 0f) {
yield return new WaitForSeconds (2f);
}
else {
yield return StartCoroutine(CutscenAmplify());
}
}
This code will stay in the first coroutine passing back and forth control with unity until the light is fully on, then it will pass control the the second coroutine which will go back and forth with unity dimming the light. Just make it do a little dance.
Little note, only start coroutines in this manor when you start the first dancer via the void Start() function. Otherwise you will have a mess.