So I am trying to make a health regeneration system but I have run into a few problems, the first one being that because time.deltatime is a float, instead of my health regenerating like it should (91-92-93 and so on) it regenerates but there are a ton of decimals (ex. 91.81238%) I was wondering how to fix this problem to have my health only regenerate in whole number increments. The other thing I am trying to do is create a 10 second delay between when the last time my character took damage, till when the regeneration actually starts. Code is below and thank you in advance.
using UnityEngine;
using System.Collections;
public class PlayerHealth : MonoBehaviour {
public int maxHealth = 100;
public float curHealth = 100;
public float regeneration = 5;
public float healthBarLength;
// Use this for initialization
void Start () {
healthBarLength = Screen.width / 2;
}
// Update is called once per frame
void Update () {
AdjustCurrentHealth (0);
if (curHealth < maxHealth)
curHealth += regeneration * Time.deltaTime;;
}
void OnGUI() {
GUI.Box (new Rect (900, 870, healthBarLength, 20), curHealth + "/" + maxHealth);
}
public void AdjustCurrentHealth(int adj) {
curHealth += adj;
if (curHealth < 0)
curHealth = 0;
if (curHealth > maxHealth)
curHealth = maxHealth;
if (maxHealth < 1)
maxHealth = 1;
healthBarLength = (Screen.width / 2) * (curHealth / (float)maxHealth);
}
}
Legend say he’s still waiting for the answer. and his players health is always full.
if you place a timestamp in one script but you take damage in the other. then your time stamp either never gets activated or is never called. so like robertbu said the answer is timestamp where you take damage. and then if you want to call the timestamp, call it in your other script.