I am currently trying to make a hunger bar that decreases over time. I know how to display the bar, I already have health bar and all, I was just wanting to know what to do with that kind of deal? I was wanting it to be something like, once every 90 seconds the amount of hunger (You start with 100) goes down by 5, but I just can’t get my head around this. I am not specifically asking for someone do to a script completely for this I just need the idea on how to make this work. Thanks!
Also I am working in C#
Try this:
void Start() {
InvokeRepeating("omnom", 0, 18.0f); //90/5 = 18, so over 90 seconds you would have lost 5 hunger.
}
int hunger = 100;
void omnom {
hunger -= 1;
}
EDIT: This is a very naive way to do this. A simpler way is given by @ExtremePowers in another answer.
A simple way to do it would be to create a timer in the Update method then just check if Time.time is 90 greater than it.
float lastHungerUpdateTime = 0f;
float currentTime;
int hunger = 100;
void Update() {
currentTime = Time.time;
if(currentTime - lastHungerUpdateTime > 90f) {
hunger -= 5;
lastHungerUpdateTime = currentTime;
}
if(hunger < 1) {
// do whatever
}
}