- Home /
Question by
Dilaxer01 · Jan 16, 2020 at 05:49 PM ·
2dtriggerfadingfading problem
Problem with sprite fading
Hello, I'm trying to make something of a "go inside-outside of a cafe fading". It works fine considering the player has entered the trigger collider and exited it by going to the left (inside of a cafe) and entered it again and exited by going to the right (outside of a cafe). But there is a bug and I don't know how to fix it: if you enter the collider and immediately decide to go the other way, it doesn't fade to what it's supposed to. (it shows you the inside of a cafe even though you are outside OR it shows you are outside of a cafe even though you are inside).
SpriteRenderer sr;
bool didTheFadeHappen = false;
void Start()
{
sr = GetComponent<SpriteRenderer>();
}
void Update()
{
}
void OnTriggerEnter2D(Collider2D player)
{
if (didTheFadeHappen == false)
{
StartCoroutine("FadeOut");
}
else
StartCoroutine("FadeIn");
}
IEnumerator FadeOut()
{
for (float f = 1f; f >= -0.1f; f -= 0.1f)
{
Color c = sr.material.color;
c.a = f;
sr.material.color = c;
yield return new WaitForSeconds(0.03f);
didTheFadeHappen = true;
}
}
IEnumerator FadeIn()
{
for (float f = 0.05f; f <= 1f; f += 0.05f)
{
Color c = sr.material.color;
c.a = f;
sr.material.color = c;
yield return new WaitForSeconds(0.03f);
didTheFadeHappen = false;
}
}
fading-problem.gif
(307.8 kB)
Comment