- Home /
Stopping Regen from Overhealing?
Hey, I am working on a regeneration function where the players health goes back to 100 when he has collected a power up. The maximum Health that the player can attain is 200.
function Regenerate ()
{
//Check if the Regeneration has set in
if(regenTime >= RegenTimerDuration)
{
//If not, start the Timer
RegenTimerDuration += 1 * Time.deltaTime;
}
//If the player has lost some Health
if(health < InitialHealth)
{
//If the timer is bigger then the Regeneration Rate,
//Start with the Regeneration process
if(regenTimer <= regenerationRate)
{
//If the first timer has set off
if(regenTime <= RegenTimerDuration)
{
//Start counting up to surpass the regenRate
regenTimer += 1 * Time.deltaTime;
}
}
}
//If the Timer is Bigger
if(regenTimer >= regenerationRate)
{
//Reset it
regenTimer = 0;
//Add to "health"
health += regenSpeed;
}
//If the Regeneration is False
if(regeneration == false)
{
//Reset the Timer
regenTimer = 0;
//And the Time to Regenerate
RegenTimerDuration = 0;
}
}
The problem: If health = 98 and regeneration is active, it will still add 4 (or whatever number regenSpeed is equal to). Can someone tell me how I would limit it to adding exactly the right amount?
Answer by CHPedersen · Aug 02, 2013 at 09:23 AM
You can use clamping to clamp it to 100, for example:
health = Mathf.Clamp(health + regenSpeed, 0, 100);
Would I have to place that anywhere specific? Or just in the function
I would put it in any position where the health variable is getting modified through the regenSpeed, e.g. this line here:
health += regenSpeed;
Like so (pretend the $$anonymous$$yPlayer. isnt there)?
//If the Timer is Bigger
if($$anonymous$$yPlayer.regenTimer >= regenerationRate)
{
//Reset it
$$anonymous$$yPlayer.regenTimer = 0;
//Add to "Health"
$$anonymous$$yPlayer.health += regenSpeed;
$$anonymous$$yPlayer.health = $$anonymous$$athf.Clamp($$anonymous$$yPlayer.health + regenSpeed, 0, 100);
}
No. The above would probably work, but you're still unnecessarily incrementing health before the clamp. This:
$$anonymous$$yPlayer.health += regenSpeed;
Is shorthand for writing:
$$anonymous$$yPlayer.health = $$anonymous$$yPlayer.health + regenSpeed;
Which, in words, says "Set the health variable of the $$anonymous$$yPlayer object to its own value, plus the value of the variable regenSpeed". You don't want to do that. You want it to say, "Set the health variable of the $$anonymous$$yPlayer object to itself plus the value of regenSpeed, but keep the value between 0 and 100". The clamping does that.
In short: Remove this line
$$anonymous$$yPlayer.health += regenSpeed;
Because it does the same as the line that uses clamp, but without clamping.
Your answer
![](https://koobas.hobune.stream/wayback/20220613111417im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Problem accessing variable in javascript 1 Answer
Accessing function from another script won't work 1 Answer
int.parse with decimal? 1 Answer
How to pass a int from a object to another 1 Answer
running code outside of update! 3 Answers