Issue with Changing vignette in coroutines

Hey, so I have a coroutine for a project that basically just sets the fade of a vignette for a simple running script. However when run the character will start sprinting the vignette will increase and then when the script tries to run the same coroutine but setting it to 0 it doesnt work. So once the character starts sprinting the vignette increases and stays on the screen forever. Running out of stamina or releasing left shift doesn’t change this

private void Update()
    {
        MyInput();
        Look();
    }

  
    private void MyInput()
    {
        x = Input.GetAxisRaw("Horizontal");
        y = Input.GetAxisRaw("Vertical");
        SprintManager();
    }

    private void SprintManager()
    {
        if ((Input.GetKey(KeyCode.LeftShift) && Input.GetKey(KeyCode.W)) && !isFatigued && currentStamina > 0)
        {
            currentStamina -= Time.deltaTime * drainRate;
            isSprinting = true;
            StartCoroutine(VignetteFade(maxVignette));
          
        }
      
        if (Input.GetKeyUp(KeyCode.LeftShift) || currentStamina <= 0)
        {
            isSprinting = false;
            StartCoroutine(VignetteFade(0));
        }

        if (currentStamina < 0)
        {
            currentStamina = 0;
            isFatigued = true;
            StartCoroutine(FatigueTimer(fatigueTime));
        }
        if ((!isSprinting && !isFatigued) && currentStamina <= maxStamina)
        {
            currentStamina += Time.deltaTime * rechargeRate;
        }
    }

and this is the VignetteFade coroutine

private IEnumerator VignetteFade(float vigAim)
    {
        float time = 0;
        Vignette vignette = postProcessing.profile.GetSetting<Vignette>();
        float vigStart = vignette.intensity.value;
        while (vigStart < vigAim)
        {
            Debug.Log("Trying to set vignette from " + vigStart + " to " + vigAim);
            time += Time.deltaTime;
            vignette.intensity.value = Mathf.SmoothStep(vigStart, vigAim, vignetteSpeed * time);
            yield return null;
          
        }
    }

I’m not very good with coroutines but any answers to this would help. I’ve tried running the VignetteFade(0) in update in an
if (!isSprinting)
but also keeps the vignette on the screen.
Thanks

Fading should NEVER be done with a coroutine, even though 99% of tutorials out there show it that way.

The reason is because what if you half-fade, then have to fade back? Coroutines make it super-awkward to start / stop, you have to check if you were doing it, how far along you were, etc. It’s a completely horrible thing.

Fortunately, fading is incredibly easy to implement without coroutines.

Fading, simple and easy:

Fading is just a special case of smoothing movement between any two particular values:

You have currentQuantity and desiredQuantity.

  • only set desiredQuantity
  • the code always moves currentQuantity towards desiredQuantity
  • read currentQuantity for the smoothed value

Works for floats, Vectors, Colors, Quaternions, anything continuous or lerp-able.

The code: SmoothMovement.cs · GitHub

1 Like