- Home /
Health Regeneration speeds up when action performed?
I have this health regeneration script, and that works fine. Except one problem where if I do something like jump health regenerates faster than it's suppose to. Here's the script:
var healthRegenRate : float = 1;
var hitPoints = 100;
function Update(){
Death();
audio.clip = Heartbeat;
DamagedTexture.active=false;
//hitPoints = Mathf.Clamp(hitPoints + (healthRegenRate * Time.deltaTime), 0, 100);
hitPoints += healthRegenRate * Time.deltaTime;
if (Input.GetButtonDown("Fire2")){
hitPoints -= 10;
}
if (hitPoints > 100){
hitPoints = 100;
}
if (hitPoints <= 15){
DamagedTexture.active = true;
if (!audio.isPlaying){
audio.Play();
}
}
if (hitPoints >= 16){
DamagedTexture.active = false;
if (audio.isPlaying){
audio.Pause();
}
}
}
I think it has something to do with the Time.deltaTime, but I'm not to sure.
I can't find anything wrong with this code, the line hitPoints += healthRegenRate * Time.deltaTime; should ensure that hitPoints is increased by one each second. In the script the healthRegenRate isn't being changed or anything so this script shouldn't be the problem.. Could you add the script in which you are calling the actions such as jump etc?
it could be Time.deltaTime remember that Time.deltaTime, as implied by the name, is simply the amount of time that has passed since the last frame.
edit: meant to type couldn't i hate english trololol thx anyways i think @merry_christmas explained a bit better
Time.deltaTime is a useful way of making a variable always increase at the same rate, since you are adding the time that has passed since the last frame to that variable each time the Update() function is called.. This means that if you added Time.deltaTime to a variable, then after one second the variable would have a value of 1 (Frame rate doesn't matter). With this in $$anonymous$$d, Time.deltaTime can't be the reason that the healthRegenRate variable is increasing faster when the player jumps :)
Answer by tmiller_vb · Jul 19, 2012 at 07:37 PM
First try changing Time.deltaTime to Time.smoothDeltaTime. This'll smooth it out so that if your framerate takes a big hit (due to jumping, moving around, or other things that'd cause a lot of rendering).
If that doesn't work, try doing this in FixedUpdate() instead of Update(). FixedUpdate happens at a fixed rate, which seems to be more of what you want. With Update, if you have lots of Draw calls, this is going to slow it down, causing the time to be inconsistent. Additionally, change Time.deltaTime to Time.fixedDeltaTime so that it will be responding to the appropriate signal.
Hopefully that works, but I can't tell because now my problem is that it regenerates too fast. And it only regenrates when I set HealthRegenRate to 50, and that's too fast.
function FixedUpdate(){
hitPoints += healthRegenRate * Time.fixedDeltaTime;
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E)){
hitPoints -= 5;
Debug.Log("HealthDown!");
}
if (hitPoints > 100){
hitPoints = 100;
}
}
Your answer
Follow this Question
Related Questions
How can i make my health regenerate? 1 Answer
Health Regeneration Issues. 2 Answers
Increase health smoothly not instantly? 2 Answers
Health Regeneration 2 Answers