Search Unity

Changing Shader Dynamically acording to my health

Discussion in 'Shaders' started by henmachuca, Oct 27, 2016.

  1. henmachuca

    henmachuca

    Joined:
    Oct 14, 2016
    Posts:
    95
    Hello,

    Is there a way to code my shader to change its diffuse color according to my health for example?!


    Thank you!
     
  2. eXonius

    eXonius

    Joined:
    Feb 2, 2016
    Posts:
    207
  3. henmachuca

    henmachuca

    Joined:
    Oct 14, 2016
    Posts:
    95
    Thank you @eXonius , but what would be the link between my health and the shader?! How could I make this work?!
     
  4. tanoshimi

    tanoshimi

    Joined:
    May 21, 2013
    Posts:
    292
    Like eXonius said...:

    Code (CSharp):
    1. float amountOfHealthLeft = 100f;
    2. void Update(){
    3.     GetComponent<Renderer>().sharedMaterial.SetFloat("_Health", amountOfHealthLeft);
    4. }

    Then do whatever you want with _Health property in the shader.
     
  5. henmachuca

    henmachuca

    Joined:
    Oct 14, 2016
    Posts:
    95
unityunity