简体   繁体   中英

Finding the remaining time when increasing a float value over time [Unity]

I have a game where a plant is growing from 0% to 100% in a certain time frame (20s in this example), the main code to execute this is in place but id like to have a way to tell how much time remains until it reaches 100% in order to display it.


public float g;
public float growthstage;
public float timeToGrow = 20; //seconds
public float timeRemaining; //to 100%


void updateGrowth(){
  g += Time.deltaTime / timeToGrow;
  growthstage = Mathf.Lerp(0, 100, g); //from 0% to 100%

  timeRemaining = ???
}

I would rather only divide by timeToGrow in Lerp , then just subtract g from timeToGrow :

g += Time.deltaTime;
growthstage = Mathf.Lerp(0, 100, g/timeToGrow); //from 0% to 100%

timeRemaining = timeToGrow - g;

But, if you need to have g be the value it is, then you could multiply it by timeToGrow before subtracting it from timeToGrow . Subjectively, I just find this a little harder to read:

g += Time.deltaTime/timeToGrow;
growthstage = Mathf.Lerp(0, 100, g); //from 0% to 100%

timeRemaining = timeToGrow - g * timeToGrow;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM