Search Unity

[Solved] Adjusting vignette value only works once - Image Effects

Discussion in 'Scripting' started by xamur, Jun 26, 2017.

  1. xamur

    xamur

    Joined:
    Mar 27, 2014
    Posts:
    109
    Hello folks,

    I am trying to achieve the simple step that the vignette increases while my player is crouching, and decreasing the value while he isn't crouching.

    Here is the code where I call the method:

    ---

    And here is my code for changing the value:

    ---

    This works only once. I am pressing my crouchKey and the vignette appears smoothly. I press the key again and the vignette disappears smoothly. Good!
    But as soon as I press the key again to crouch, the vignette instantly changes to the other value, but if I press the key to stand up it disappears smoothly again.

    What could be the problem here?
    Thank you!
     
    Last edited: Jun 27, 2017
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    I suspect it is because you have multiple ChangeVignetteValue running. Let's walk through it.

    First crouchkey. ChangeVignetteValue runs once. Enters crouching while loop.

    Second crouchkey first ChangeVignetteValue exits and a new one starts, entering !crouching while loop!

    Third crouchkey. Second ChangeVignetteValue exits !crouching while, but then enters crouching while. A third ChangeVignetteValue starts which also enters the crouching while. You now have two while loops running.

    Now the second one sets it to full because it's time.time - timeToStart returns a high enough value.
     
    Last edited: Jun 26, 2017
    xamur likes this.
  3. xamur

    xamur

    Joined:
    Mar 27, 2014
    Posts:
    109
    EDIT: Ah I got it. You're right, the coroutine was running multiple times. I just added a few more lines to stop it if it is already running. Thanks! :)

    So easy, but sometimes you just don't know what to do.
     
    Last edited: Jun 27, 2017
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Glad you got it working! And understood what I was trying to explain. :)
     
    xamur likes this.