Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Kelvik · Apr 18, 2014 at 05:36 AM · c#delayregenerationhealth

[C#]Health Regeneration and Delay

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);
 
 
         }
 
 
 }
 

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by getyour411 · Apr 18, 2014 at 05:55 AM

I do it like this; you'd have to fill in the blanks and modify to your set up

 void Update() {

     // If game is paused, don't do any of this
     if (QM_Utils.gamePaused)
                     return;

     if(curHealth != maxHealth && !isRegenHealth) {
         StartCoroutine(RegainHealthOverTime());
     }
 }


     private IEnumerator RegainHealthOverTime() {
     isRegenHealth = true;
     while (curHealth < maxHealth) {
         AdjustCurrentHealth (1);
         CalcHealthPct (curHealth);
         yield return new WaitForSeconds (healthRegenRate);
             }
     isRegenHealth = false;
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by robertbu · Apr 18, 2014 at 05:50 AM

In Start() put:

 InvokeRepeating("Regenerate", 0.0f, 1.0 / regeneration);

Then write the function Regenerate():

 void Regenerate() {
     if (curHealth < maxHealth)
              curHealth += 1.0f;
 }

Or if you make curHealth and 'int', you can just do curHealth++.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kelvik · Apr 18, 2014 at 06:10 AM 0
Share

Now what could I add to only make this execute if I have not taken damage for 10 seconds?

avatar image robertbu · Apr 18, 2014 at 06:25 AM 1
Share

At the top of the file put:

 private float timestamp = 0.0;

Then when you take damage do:

 timestamp = Time.time;


Then Regenerate() becomes:

 void Regenerate() {
     if (curHealth < maxHealth && Time.time > (timestamp + 10.0f))
              curHealth += 1.0f;
 }
avatar image Kelvik · Apr 18, 2014 at 10:27 PM 0
Share

Cant seem to get it to work because my health and damage script are two different scripts, right now it waits 10 seconds after I take damage to begin regenerating, but if i take damage again after the initial 10 seconds, it keeps regenerating. Both scripts included below.

 public class PlayerHealth : $$anonymous$$onoBehaviour {
     public int maxHealth = 100;
     public float curHealth = 100;
     public int regeneration = 5;
 
     private float timestamp = 0.0f;
 
 
     public float healthBarLength;
 
     // Use this for initialization
     void Start () {
         InvokeRepeating("Regenerate", 0.0f, 10.0f / regeneration);
         healthBarLength = Screen.width / 2;
 
     }
 
     // Update is called once per frame
     void Update () {
                 
         AdjustCurrentHealth (0);
     
 
 
     }
 
     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);
 
 
         }
     void Regenerate() {
         if (curHealth < maxHealth && Time.time > (timestamp + 10.0f))
             curHealth += 1.0f;
     }
 
 
 }


























 using UnityEngine;
 using System.Collections;
 
 public class EnemyAttack : $$anonymous$$onoBehaviour {
     public GameObject target;
     public float attackTimer;
     public float coolDown;
 
     
     // Use this for initialization
     void Start () {
         attackTimer = 0;
         coolDown = 2.0f;
     }
     
     // Update is called once per frame
     void Update () {
         if (attackTimer > 0)
             attackTimer -= Time.deltaTime;
         
         if (attackTimer < 0)
             attackTimer = 0;
         
 
         if (attackTimer == 0) {
             Attack ();
             attackTimer = coolDown;
                 
         }
         
     }
     
     private void Attack() {
         float distance = Vector3.Distance (target.transform.position, transform.position);
         
         Vector3 dir = (target.transform.position - transform.position).normalized;
         
         float direction = Vector3.Dot (dir, transform.forward);
         
         Debug.Log (distance);
         
         if (distance < 3f) {
             if (direction > 0) {
                 PlayerHealth eh = (PlayerHealth)target.GetComponent ("PlayerHealth");
                 eh.AdjustCurrentHealth (-10);
 
                 
             }
         }
 
     }
 }
 
avatar image Mmrw · Jul 03, 2015 at 10:36 AM 1
Share

$$anonymous$$ake timestamp = 0 after curhealth += 1.0f

avatar image
1

Answer by Sefcio · Jun 23, 2015 at 08:01 PM

This is great, based on what you did I made my part of this script like this:

 public class Health : MonoBehaviour {
 
     public int startingHealth = 100;
     public int currentHealth;
     public int healthReg;
 
     bool isRegenHealth;
 
  
     void Awake ()
     {
         currentHealth = startingHealth;
     }
 
 
     void Update() 
     {
         if(currentHealth != startingHealth && !isRegenHealth) 
         {
             StartCoroutine(RegainHealthOverTime());
         }
     }
 
 
     private IEnumerator RegainHealthOverTime() 
     {
         isRegenHealth = true;
         while (currentHealth < startingHealth) 
         {
             Healthregen ();
             yield return new WaitForSeconds (1);
         }
         isRegenHealth = false;
     }
 
 
     public void Healthregen() 
     {
         currentHealth += healthReg;
         
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# Health regeneration delay after hit 0 Answers

Why don't my send messages work? 2 Answers

If health = 0, show a GUI.button C# 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges