- Home /
Lerp doesn't work with SetAlpha
Hi, I am using a UI panel and this script in the main camera to make fade in/out effects, but the fade time is not in seconds. For example if it's fadetime is 5f, the fade takes a lesser time.
I've been stuck here for the past three hours. Any help? I would appreciate it so much. Here is the piece of code I've written for it:
[SerializeField] private CanvasRenderer panel;
private void Awake() => maincamera = Camera.main;
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
Fadeout(15f);
}
}
public void Fadein(float fadetime) => StartCoroutine(Fadi(fadetime, false));
public void Fadeout(float fadetime) => StartCoroutine(Fadi(fadetime, true));
private IEnumerator Fadi(float fadetime, bool fadeout)
{
panel.gameObject.SetActive(true);
float timma = 0f;
float currentpanelalpha = fadeout ? 0f : 100f;
float newpanelalpha = Mathf.Abs(currentpanelalpha - 100f);
panel.SetAlpha(currentpanelalpha);
while (timma < fadetime)
{
panel.SetAlpha(Mathf.Lerp(currentpanelalpha, newpanelalpha, timma / fadetime));
timma += Time.deltaTime;
yield return null;
}
panel.SetAlpha(newpanelalpha);
}
Answer by rh_galaxy · Mar 24 at 12:21 AM
The only thing I find wrong with your code is that alpha should be between 0.0f and 1.0f. You should also guard against that the user can start multiple Fadeout() by clicking again before the fade is done.
float currentpanelalpha = fadeout ? 0f : 1f;
float newpanelalpha = fadeout ? 1f : 0f;
Your answer
Follow this Question
Related Questions
Fade logo before main menu 2 Answers
How to make a float incrementally go up, reach the top, then go back down again and stop 1 Answer
Fade In / Out UI Image 3 Answers
Changing a GUITexture's alpha gives me no effect in game. 0 Answers
alpha value not changing(gradual decrease) during runtime 1 Answer