Home>

I want to make a process that when the player falls off the stage, the screen is darkened (fade out) for a moment, returned to the starting position, and then faded in. For that purpose, I made the following function.

However, the following process will behave as if the process of returning the player and fade-in are performed twice. I think it is probably because i am not used to coroutines.

private IEnumerator salvation (float fadeOutTime, float fadeInTime = 0.1f)
    {
        float time = 0;
        while (true)
        {
            Color color = m_fadePlane.GetComponent<Image>(). Color;
            color.a = time/fadeOutTime;
            m_fadePlane.GetComponent<Image>(). color = color;
            time + = Time.deltaTime;
            if (color.a>1f)
            {
                break;
            }
            yield return null;
        }

        // Process to return the player to its original position

        time = 0;
        while (true)
        {
            Color color = m_fadePlane.GetComponent<Image>(). Color;
            color.a = 1f-time/fadeOutTime;
            m_fadePlane.GetComponent<Image>(). color = color;
            time + = Time.deltaTime;
            if (color.a<0f)
            {
                break;
            }
            yield return null;
        }

    }


And where you call this function

StartCoroutine (salvation (fadeOutTime, fadeInTime));


Is calling.

At this time, it works well, but it behaves as if the player was restored and faded in twice. I think it is probably because i am not used to coroutines.

  • Answer # 1

    First, change the salvation function as follows (added isSalvated property)

    bool isSalvated {get;private set;}
    private IEnumerator salvation (float fadeOutTime, float fadeInTime = 0.1f)
        {
            float time = 0;
            isSalvated = true;
            while (true)
            {
                Color color = m_fadePlane.GetComponent<Image>(). Color;
                color.a = time/fadeOutTime;
                m_fadePlane.GetComponent<Image>(). color = color;
                time + = Time.deltaTime;
                if (color.a>1f)
                {
                    break;
                }
                yield return null;
            }
            // Process to return the player to its original position
            time = 0;
            while (true)
            {
                Color color = m_fadePlane.GetComponent<Image>(). Color;
                color.a = 1f-time/fadeOutTime;
                m_fadePlane.GetComponent<Image>(). color = color;
                time + = Time.deltaTime;
                if (color.a<0f)
                {
                    break;
                }
                yield return null;
            }
          isSalvated = false
        }

    And in the caller of StartCoroutine

    if (! ****. Instance.isSalvated) {
    StartCoroutine (salvation (***, ***));
    }

    does not allow you to go through the salvation function during the transition