- Home /
Health Regeneration Issues.
I'm trying to implement a SLOW health regeneration system... That will regenerate 1 health per 1 second. This is what i have in my health script ATM.
var regen : float = 1;
function Update () {
if(hitPoints < maxHitPoints) {
hitPoints += regen * Time.deltaTime;
}
}
This code works but only when i put my regen variable above 47 and that is ridiculously fast. I have searched through answers.unity3d for a while but couldn't find a fix... Hopefully someone here knows how to fix this issue. Thanks for your time.
Answer by Griffo · Aug 01, 2013 at 06:55 AM
var regen : float = 1;
var loop : boolean;
function Update () {
if(hitPoints < maxHitPoints && !loop) {
Health();
}
}
function Health(){
loop = true;
yield WaitForSeconds (1);
hitPoints ++;
loop = false;
}
Try this ..
No it doesn't... Same thing as your first post.. Its smooth but makes health restore instantly.
Yes it does .. Ive tested it on an empty GameObject, I set maxHitPoints in the inspector to 20 then watched hitPoints increase from 0 by 1 every second until it reached 20 in the inspector ..
var regen : float = 1;
var loop : boolean;
var hitPoints : float;
var maxHitPoints : float;
function Update () {
if(hitPoints < maxHitPoints && !loop) {
Health();
}
}
function Health(){
loop = true;
yield WaitForSeconds (1);
hitPoints ++;
loop = false;
}
Okay Buddy... I just tryed the code you posted and yes it works... The problem i had i didn't see && !loop)
Now This Works 100% and i can give u +1 and accept answer, Thanks for your time and i really appreciate it :) Thank you!
Answer by Shar1ngan · Aug 01, 2013 at 07:08 AM
var timer : float = 0;
var regenPerSecond : float = 1;
function Update () {
timer += Time.deltaTime;
if(hitPoints < maxHitPoints)
{
if(timer >= 1)
hitPoints += regenPerSecond;
timer = 0;
}
}
}
or try this (without regen)
function Update () {
if(hitPoints < maxHitPoints) {
hitPoints += Time.deltaTime;
}
}
Sum of Time.deltaTime in per second = 1 as i know...
@Share1ngan, No idea what you mean... Not One Of The Above Worked.
Probably because maxHitPoints is not a percentage of the total XP
if you have 2000 hp, with this code, it will recover very slowly. (or if maxHitPoints = 10, it will recover very fast)
Try set in Start or other function (where you set maxHitPoints value)
regenPerSecond = maxHitPoints / 100;
then will regenerate 1% per second with first code.
When the timer hits 0. The timer doesn't reCount so it only gives 1 health ever..
Your answer
Follow this Question
Related Questions
Health Regeneration speeds up when action performed? 1 Answer
Add 1 Per Second to an Int 1 Answer
How can i make my health regenerate? 1 Answer
how to change the filedof view slowly 2 Answers