Search code examples
c#unity-game-engineparticle-system

Smoke Opacity Relative to Player Health


I have a particle system emitting smoke. I am attempting to make the apparent thickness or darkness of the smoke relative to the health of the player. To do this, I am applying the relative vehicle damage (0 to 100) to the alpha of the particle system start color. Even when I divide the player damage by some number (6 is what the snipped below uses), the darkness of the smoke sees way beyond what I expect. For example, an alpha of 30 looks as dark as 255. Any thoughts? Better way to do this?

var smokeColor = smokeParticles.startColor;
float smokeAlpha = (255 / 100) * (vehicleDamage / 6);
if (smokeAlpha > 255)
{
    smokeAlpha = 255;
}

print("smoke alpha " + smokeAlpha.ToString());
var newcolor = new Color(smokeColor.r, smokeColor.g, smokeColor.b, smokeAlpha);
smokeParticles.startColor = newcolor;

Solution

  • You are passing 255 or values that are more than 1f to a parameter that expects 0f to 1f values. That's why its not working. 1f is completely opaque and alpha of 0 is completely transparent. Make smokeAlpha a value between 0f and 1f then pass it to your new Color then it should work.

    Your smokeColor.r,smokeColor.g and smokeColor.b looks fine because you are getting them from the smokeParticles variable and you are not modifying them like the way you are modifying the alpha variable(smokeAlpha).

    If you really want the smokeAlpha variable to use the 0 to 255 value range then you have to divide your final value by 255.

    var smokeColor = smokeParticles.startColor;
    float smokeAlpha = (255 / 100) * (vehicleDamage / 6);
    if (smokeAlpha > 255)
    {
        smokeAlpha = 255;
    }
    
    smokeAlpha = smokeAlpha / 255f; //Add this
    
    print("smoke alpha " + smokeAlpha.ToString());
    var newcolor = new Color(smokeColor.r, smokeColor.g, smokeColor.b, smokeAlpha);
    smokeParticles.startColor = newcolor;