Two floats == Percentage

I feel like this should be a very easy task, the more I try new things and more research I do, the more confused I get.

The code below is from one script, the other script is essentially what holds my TextMesh(s). The way it’s set up, CurrentMeter goes from 0% to 100%. Once it reaches 100% it jumps directly back to 0%.

What i’m trying to do is: I want to flip the percentages so it starts at 100%, goes down to 0%. When at 0% I want it to gradually climb back up to 100% at the same pace as the Time.deltaTime.

public float meterTime = 0.0F;
public float meterMax = 5.0F;

void CheckMeter()
{
    meterTime += Time.deltaTime;

    if(meterTime >= meterMax)
    {
         //Activate something by boolean

         meterTime = 0;
    }
}

void UpdateMeter()
{
    CurrentMeter = (meterTime / meterMax) * 50;

    ModifyMeter (CurrentMeter)     //<---- linked to a HUD Script
}

This is my very first post on here, so if my format is incorrect or sloppy i’m sorry. Id rather someone point out the issue and let me know so I won’t do it again.

If I’ve got it right (and it’s late where I am!) then you’re re-setting meterTime to 0.

Really meterTime is just a float so when you get to 100% meterMax (or just above) you need to change meterTime += Time.deltaTime to meterTime -= Time.deltaTime
Until it gets to 0 again.

set a bool for positive or negative bool PosNeg and if true do meterTime += if false do meterTime -=

You’ll also need a meterLow to handle when it reaches 0 (or just use 0)

Another way is to just have a INT specifying which direction its going…

int direction = 1;
meterTime += Time.deltaTime * direction;

When you want to change direction, just do

direction *= -1;

Some simple mathematical tricks that will help.

  • % operator. This returns the remainder after division.
  • 1 - . This can be used to flip a value.

No sure is % is appropriate for your code. But you can implement the subtraction simply by

CurrentMeter = (1 - meterTime / meterMax) * 50; // Why 50? Percent is 100