Question by
nickkel95 · Mar 06, 2019 at 02:26 PM ·
playerprogrammingintegerhealth
adding two integers together once every half second,adding two integers together with delay?
Hey, so I'm currently writing a base player script, and have been looking around to see if I can add two integer values together once every second, or half a second and for the lfe of me, I can't find anything around, if someone can help me with this, I'd be eternally grateful!
got the current code here:
public void Update()
{
if (curhealth < health)
{
for (int timeBinter = 0; < 500; timeBinter++) ;
{
if (timeBinter == 500)
Regenstat = true;
if (curhealth == health)
Regenstat = false;
if (Regenstat == true)
curhealth + hpRegen;
}
So I need to have "curhealth" and "hpRegen" add together once every second or every specified interval.
Comment
Best Answer
Answer by Hellium · Mar 06, 2019 at 02:37 PM
public float RegenInterval = 1; // Will give you `hpRegen` every 1 second
private float regenTimer;
public void Update()
{
if (curhealth < health)
{
// Increase the timer
regenTimer += Time.deltaTime;
// If threshold has been passed,
// increase health and make timer "loop"
if( regenTimer > RegenInterval )
{
regenTimer -= RegenInterval ;
// Make sure the current health isn't greater than the maximum health
curhealth = Mathf.Min( curhealth + hpRegen, health );
}
}
else
{
regenTimer = 0;
}
}