x


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.

more ▼

asked Jul 19 '12 at 10:03 AM

chrisall76 gravatar image

chrisall76
136 5 9 15

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?

Jul 19 '12 at 11:03 AM merry_christmas

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

Jul 19 '12 at 03:48 PM Mander

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 mind, Time.deltaTime can't be the reason that the healthRegenRate variable is increasing faster when the player jumps :)

Jul 19 '12 at 04:39 PM merry_christmas
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Jul 19 '12 at 07:37 PM

tmiller_vb gravatar image

tmiller_vb
21 1 2 2

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.GetKeyDown(KeyCode.E)){
    hitPoints -= 5;
    Debug.Log("HealthDown!");
}
if (hitPoints > 100){
    hitPoints = 100;
}
}
Jul 20 '12 at 02:33 AM chrisall76
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x588
x394
x355
x6

asked: Jul 19 '12 at 10:03 AM

Seen: 328 times

Last Updated: Jul 20 '12 at 02:34 AM