Change the color of material based on percentage of health remaining
Hey guys, I'm a bit stuck on a specific implementation. I want to have a character's health represented by tinting the material color of the character based on the percentage of health remaining. I know the entire material color can be changed using GetComponent<Renderer>().material.color = Color.red;
, but I'm not sure how to only change a portion of the color based on a percentage. Attached is an image of the effect I'm going for. Thanks for any and all help!
If I understand correctly, you want the character to have an colored "overlay" representing its life? Like if the whole character was a vertical healthbar ?
What you want to achieve is not possible without a custom shader.
Answer by tormentoarmagedoom · Apr 26, 2018 at 12:32 PM
Good day,
You can declare a color by its 4 components (Red, green, blue, transparency) represented from 0 to 1, for example
GetComponent<Renderer>().material.color = new color(0.3f, 1, 0, 0.2f)
So you can replace a number for a float variable (lile health).
Byre!!
Accept the answer!