Having the code typed exactly like in the course I’m trying to figure out why my UI image fades to each end of the alpha almost instantly instead of gradually like in the course video. Not sure if I’ve explained that well enough.
You’d have to share your fade code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UIFade : Singleton
{
[SerializeField] private Image fadeScreen;
[SerializeField] private float fadeSpeed = 1f;
private IEnumerator fadeRoutine;
public void FadeToBlack()
{
if(fadeRoutine != null)
{
StopCoroutine(fadeRoutine);
}
fadeRoutine = FadeRoutine(1);
StartCoroutine(fadeRoutine);
}
public void FadeToClear()
{
if(fadeRoutine != null)
{
StopCoroutine(fadeRoutine);
}
fadeRoutine = FadeRoutine(0);
StartCoroutine(fadeRoutine);
}
private IEnumerator FadeRoutine(float targetAlpha)
{
while(!Mathf.Approximately(fadeScreen.color.a, targetAlpha))
{
float alpha = Mathf.MoveTowards(fadeScreen.color.a, targetAlpha, fadeSpeed * Time.fixedDeltaTime);
fadeScreen.color = new Color (fadeScreen.color.r, fadeScreen.color.g, fadeScreen.color.b, targetAlpha);
yield return null;
}
}
}
And then FadeToBlack() and FadeToClear() are just called in an AreaExit and AreaEntrance script with one more Coroutine in AreaExit:
private IEnumerator LoadSceneRoutine()
{
while (waitToLoadTime >= 0)
{
waitToLoadTime -= Time.fixedDeltaTime;
yield return null;
}
SceneManager.LoadScene(sceneToLoad);
}
}
Apologies for not including the code before. Thanks for any input
Your problem is here
You calculate the next alpha
value, but you set the fadeScreen.color
's alpha value equal to the targetAlpha
. You should be setting it to alpha
Thank you very much. That small change definitely worked. I’ll have to go through and double check the video to see if I’m just blind. Thank you for your input and your time.
I checked the code in the repo. It’s alpha
there, so the video should definitely have it
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.