Transparency coroutine

Hello!

im in Unity 2D rpg combat system lecture, and at environment sprite/tilemap transparency we made coroutines for fading in and fading out in a set amount of time.
when i move under the canopy(leaf tiles) and wait for it to fade, move out, and move right back in, it does not properly fade smoothly, but fade instantly.
is it because it is trying to do the coroutine multiple times since we have the routinestart on TriggerEnter2D and TriggerExit2D?
i tried to think of a solution but after an hour i gave up :frowning:
the game functions perfectly by the way, this does not hinder the game experience, it is something that only bothers me for sure.
so i guess my question is, how can we fix this?

sorry if my explanation isnt that detailed and thanks for the answers in advance!

I haven’t done this course yet, but this is a common issue. The OnTriggerExit2D started to fade in the canopy (and is still running) when the OnTriggerEnter2D starts to fade it out again. Now you have 2 coroutines competing against each other and the fade gets nowhere for the time it’s competing because one is going down and another going up leaving it at the same value. When the first finally finishes, there’s only a small amount of time left to do the rest of the second so it fades too fast, making it appear to be instant.

One way to fix it is to stop the currently running coroutine before starting a new one. Since this script only do the fades and no other coroutines, the simplest way is to just call StopAllCoroutines() before starting a new one.

2 Likes

This is correct. The Coroutines are competing against one another.

Adding StopAllCoroutines immediately before the invocation of a coroutine should fix this:

    private void OnTriggerEnter2D(Collider2D other) {
        if (other.gameObject.GetComponent<PlayerController>()) {
            if (spriteRenderer) {
                StopAllCoroutines();
                StartCoroutine(FadeRoutine(spriteRenderer, fadeTime, spriteRenderer.color.a, transparencyAmount));
            } else if (tilemap) {
                StopAllCoroutines();
                StartCoroutine(FadeRoutine(tilemap, fadeTime, tilemap.color.a, transparencyAmount));
            }

            foreach (Collider2D bleedPreventionCollider in bleedPreventionColliders)
            {
                bleedPreventionCollider.enabled = true;
            }
        }
    }

    private void OnTriggerExit2D(Collider2D other) {
        if (other.gameObject.GetComponent<PlayerController>())
        {
            if (spriteRenderer) {
                StopAllCoroutines();
                StartCoroutine(FadeRoutine(spriteRenderer, fadeTime, spriteRenderer.color.a, 1f));
            } else if (tilemap) {
                StopAllCoroutines();
                StartCoroutine(FadeRoutine(tilemap, fadeTime, tilemap.color.a, 1f));
            }
            foreach (Collider2D bleedPreventionCollider in bleedPreventionColliders)
            {
                bleedPreventionCollider.enabled = false;
            }
        }
    }
1 Like

Thank you for both the explanation and the solution, it works perfectly!

cheers!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms