Search Unity

Bug Issue with Changing vignette in coroutines

Discussion in 'Scripting' started by GibbyGib, Mar 15, 2022.

  1. GibbyGib

    GibbyGib

    Joined:
    Oct 6, 2020
    Posts:
    1
    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
    Code (CSharp):
    1.    
    2. private void Update()
    3.     {
    4.         MyInput();
    5.         Look();
    6.     }
    7.  
    8.  
    9.     private void MyInput()
    10.     {
    11.         x = Input.GetAxisRaw("Horizontal");
    12.         y = Input.GetAxisRaw("Vertical");
    13.         SprintManager();
    14.     }
    15.  
    16.     private void SprintManager()
    17.     {
    18.         if ((Input.GetKey(KeyCode.LeftShift) && Input.GetKey(KeyCode.W)) && !isFatigued && currentStamina > 0)
    19.         {
    20.             currentStamina -= Time.deltaTime * drainRate;
    21.             isSprinting = true;
    22.             StartCoroutine(VignetteFade(maxVignette));
    23.          
    24.         }
    25.      
    26.         if (Input.GetKeyUp(KeyCode.LeftShift) || currentStamina <= 0)
    27.         {
    28.             isSprinting = false;
    29.             StartCoroutine(VignetteFade(0));
    30.         }
    31.  
    32.         if (currentStamina < 0)
    33.         {
    34.             currentStamina = 0;
    35.             isFatigued = true;
    36.             StartCoroutine(FatigueTimer(fatigueTime));
    37.         }
    38.         if ((!isSprinting && !isFatigued) && currentStamina <= maxStamina)
    39.         {
    40.             currentStamina += Time.deltaTime * rechargeRate;
    41.         }
    42.     }
    and this is the VignetteFade coroutine
    Code (CSharp):
    1.  
    2. private IEnumerator VignetteFade(float vigAim)
    3.     {
    4.         float time = 0;
    5.         Vignette vignette = postProcessing.profile.GetSetting<Vignette>();
    6.         float vigStart = vignette.intensity.value;
    7.         while (vigStart < vigAim)
    8.         {
    9.             Debug.Log("Trying to set vignette from " + vigStart + " to " + vigAim);
    10.             time += Time.deltaTime;
    11.             vignette.intensity.value = Mathf.SmoothStep(vigStart, vigAim, vignetteSpeed * time);
    12.             yield return null;
    13.          
    14.         }
    15.     }
    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
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,736
    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:

    https://forum.unity.com/threads/how...color-with-duration-time.969864/#post-6311295

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

    https://forum.unity.com/threads/beginner-need-help-with-smoothdamp.988959/#post-6430100

    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: https://gist.github.com/kurtdekker/fb3c33ec6911a1d9bfcb23e9f62adac4
     
    GibbyGib likes this.