I’m sure I’m missing something stupid, but I’m having a big problem.
Right now I’m using the Transparent Cutoff trick to create a health bar from alpha. Works fine. At 0 _Cutoff, the bar is full, and at 1, the bar is totally cut off.
This is completely backwards however from how the health works (and indeed how 99% of all our status bars would work, I imagine), where 100 is full and 0 is empty. The problem is, how can I correlate my 100% health with 0% _Cutoff, and vice-versa? This logistical nightmare has been plaguing me for the past hour.
Is there some sort of math function that can help me out here? Am I doing something wrong? Missing something simple?
Thanks.
2 Answers
2
The easiest way is just to subtract your value from one:
_Cutoff = 1.0 - (currentHealth / maxHealth);
that “inverses” the value.
If you want a math function, then I suggest Mathf.InverseLerp(). This calculates the lerp parameter given a value between a max and a min.
example:
//Mathf.InverseLerp(from , to , value) returns the parameter that would generate value given a lerp:
//Mathf.Lerp(from , to , parameterFromPrev) returns value;
//example:
var lerpVal = Mathf.InverseLerp( 0 , 150 , 75);
//returns .5
so in your case you want to do:
_Cutoff = Mathf.InverseLerp(maxHealth , minHealth , currentHealth );
I think something like this would work:
`alpha = 1.0-health`
also I think you can do it like _Cutoff = (maxHealth-currentHealth)/100;
– Uriel_96that should work too. "(maxHealth - curHealth) / maxHealth" just to make it foolproof though. :)
– Peter_GAh, cool. Thanks for your help, Peter. I'll mark your answer if all goes well.
– anon25104582Your answer was just what I needed. Either way works that you and Uriel posted. I feel stupid for asking now. :) My brain just had a logistical burp.
– anon25104582