I am trying to progressively shift the color of the enemy to red, but something in the code is not working. I tested it without the if statements and it worked, so i suppose i got something wrong with those. This is the code:
void OnCollisionEnter2D(Collision2D coll)
{
if (coll.gameObject.tag=="Bullet") {
float result = (health / maxHealth);
if (0.9f < result && result <= 1.0f) rend.material.color = new Color(255, 239, 239);
else if (0.8f < result && result <= 0.9f) rend.material.color = new Color(255, 207, 207);
else if (0.7f < result && result <= 0.8f) rend.material.color = new Color(255, 175, 175);
else if (0.6f < result && result <= 0.7f) rend.material.color = new Color(255, 143, 143);
else if (0.5f < result && result <= 0.6f) rend.material.color = new Color(255, 111, 111);
else if (0.4f < result && result <= 0.5f) rend.material.color = new Color(255, 79, 79);
else if (0.3f < result && result <= 0.4f) rend.material.color = new Color(255, 63, 63);
else if (0.2f < result && result <= 0.3f) rend.material.color = new Color(255, 47, 47);
else if (0.1f < result && result <= 0.2f) rend.material.color = new Color(255, 31, 31);
else if (0.0f < result && result <= 0.1f) rend.material.color = new Color(255, 15, 15);
}
}
Thanks for the attention!
I’m too lazy to check all the ifs, but this should do the same:
Color color1 = new Color( 1.0f, 0.06f, 0.06f );
Color color2 = new Color( 1.0f, 0.94f, 0.94f );
rend.material.color = Color.Lerp(color1, color2, (float)health / maxHealth);
Hi,
I believe the Color statement should be based on units of 1 and not 255… for example new Color(255, 239, 239) is in fact be new Color(1.0f, 0.937f, 0.937f, 1.0f) - don’t forget the last 1 for the alpha.
I didn’t test the answer posted by doublemax but if it works it is nice code which I would recommend you implement. I would have gone for a switch statement (Unity Connect) but it is still more lines and perhaps more calculations that what is suggested.
In anycase you need to fix your color statements or use a color32 one
hope that helps,
Hi there!
Dont know if you have already sloved your problem (Sorry for BAD english) but if not tyr to use
rend.material.color = new Color32( byte r, byte g, byte b, byte a );`
Some time ago i was having the same trouble, colors just didn`t looked like I was needed to.
All these if statements are tedious and unneeded. Can you imagine trying to change the colors to something different if you change your mind?! Heres what I would do, everything can be simplified to 1 line of code and a gradient. The cool thing about using a gradient rather than just lerping between 2 colors is that you can have the color shift to something different halfway. 0 could be red, 0.5 could be green, 1.0 could be blue.
// Start by declaring a gradient and set it up in the inspector with your desired color values.
// 0.0 is the left side of the gradient, 1.0 is the right side of the gradient
public Gradient grad;
// since you are taking the current health and dividing by max health,
// this is perfect for evaluating the gradient.
// Evaluating a gradient needs to be between 0.0 and 1.0
// now just apply to the material
rend.material.color = grad.Evaluate((float)health / (float)maxHealth);