Search Unity

Triggering Events in Shader Graph?

Discussion in 'Shaders' started by LukeZeches, Jul 16, 2018.

  1. LukeZeches

    LukeZeches

    Joined:
    Jul 12, 2018
    Posts:
    5
    So in my game when the player's health reaches 0 I want the player to dissolve. I've made a really basic shader for this. It uses sine of Time and kind of oscillates between being transparent and not. I can switch to it no problem, but when I do the shader always seems to be partway through the dissolving animation making it look sloppy. Is there anyway for me to set up something in one of my scripts so that the dissolve animation only triggers when the player's health reaches 0?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    If you want your shader to animate starting when the material is swapped you'll need to animate the fade out by modifying a material property from script or with an animation. Or by setting a custom start time property from script that you subtract from the shader's time.

    The time node is the same time value as in script side Time.timeSinceLevelLoad, so if you set a material property to that value when you swap, and subtract it from the Time node's output you'll get a value that is 0.0 on the first frame, then increases in value every second. If you want the fade to be the first part of a sine wave, you'll need to use that value as an input into a Sine node instead of using the Time node's sine time, however you might want to use a Smoothstep instead which also has a ease in and out, but returns a 0 to 1 value and clamps rather than repeats. You also just need to know how many seconds you want the effect to take rather than converting to radians when using a trig function.