Floats/Integers that are linked but opposite?

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 ); 

also I think you can do it like _Cutoff = (maxHealth-currentHealth)/100;

–

that should work too. "(maxHealth - curHealth) / maxHealth" just to make it foolproof though. :)

–

Ah, cool. Thanks for your help, Peter. I'll mark your answer if all goes well.

–

Your 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.

–

I think something like this would work:

`alpha = 1.0-health`

Instead of having a duplicate object to make both sides of the wall render, you should download this https://assetstore.unity.com/packages/vfx/shaders/free-double-sided-shaders-23087 that should work for rendering both sides... but I still can't figure out what the problem is...

–