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);
}