- Home /
What would be the best approach for regenerating stats?
I'm scripting player stats and was wondering what would be the best approach for regenerating health/stamina/magic? what would be the easiest and/or what would be the most efficient?
Something like this?
float maxhealth = 100; //maximum health
float curhealth = maxhealth; //current health
void SomeMethod(){
InvokeRepeating("RegenHealth", 1, 1);
}
void RegenHealth(){
if (curhealth < maxhealth){
curhealth += 2;
}
if(curhealth >= maxhealth){
curhealth = maxhealth;
CancelInvoke();
}
Or something like this?
void Update(){
if(curhealth < maxhealth){
curhealth += 2 * Time.deltatime;
if(curhealth >= maxhealth){
curhealth = maxhealth;
}
}
}
Or is there another way of doing it? Answers appreciated.
Answer by The Kracken · Dec 10, 2013 at 01:58 AM
Both versions work just fine. I'd say that the first method is what i would use, as you can add effects to it as well such as floating icons that say +2 over a health bar or what not. The second one is a bit easier to read and you have a finer granularity to how much health is being recovered. The player might only get back 1.5hp before they get attacked rather then a straight 2hp/sec.
All in all they both work great and both have there pro/cons.
Ok thanks. I'll go with the first one. Though should the InvokeRepeating be put in the Update method? Would it get called multiple times?
Nah leave it out of update. It will continue to get called until you cancel the invoke.
Your answer
Follow this Question
Related Questions
Using InvokeRepeating in the Update method? 2 Answers
Recharge delay on shield regen through coroutines 2 Answers
InvokeRepeating not executing - recently updated unity 1 Answer
InvokeRepeating OR Update OR FIxedUpdate? 2 Answers
Prefab Instantiation 1 Answer