• 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 fiterpilot · Aug 03, 2017 at 07:03 AM · damagesystemhealthbarhealth

Health Regen Stop After Damage

I'm working on a health regen system for a game and I'm running into some issues. Seems no one else has tried this as far as I can tell so I HAVE Googled it already.

Anyway I want the system to act like this: When damage is received, wait x seconds and and then start the regen process. If damage is received WHILE regening, stop the regen process and start over.

I can't quite figure out how exactly to do this. Here is my code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class HealthScript : MonoBehaviour {
 
     int maxHealth = 100; //Player's Max health
     public float currentHealth; //Player's Current health
     public float regenSpeed; //Regen speed (wait time)
     public float regenWaitTime; //Time before regen starts
     bool isRegen = false; //If regening
     bool canRegen = true; //If can regen
 
     void Start(){
         currentHealth = maxHealth; //Sets player health
     }
     
     void Update () {
         if(currentHealth>maxHealth){
             currentHealth = maxHealth;
         }else if(currentHealth!=maxHealth && !isRegen){ //If they have health less than max
             StartCoroutine(Regen()); //Start regen
         }
     }
 
     //Function for health regen
     IEnumerator Regen(){
         isRegen = true; //Set regenning to true
         yield return new WaitForSeconds(regenWaitTime); //Wait for delay
 
         while(currentHealth<maxHealth){ //Start the regen cycle
                 currentHealth += 1; //Increase health by 1
                 yield return new WaitForSeconds(regenSpeed); //Wait for regen speed
         }
         isRegen = false; //Set regenning to false
     }
 
     //Function for dying
     void Die(){
         Debug.Log(this.name+" died."); //This is where death cycle will go
         canRegen = false;
     }
 
     //Function for taking damage
     public void TakeDamage(float damage){
         if(currentHealth<=0){
             Die();
         }else{
             currentHealth -= damage; //Remove health
         }
     }
 }
 
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by ShadyProductions · Aug 03, 2017 at 07:08 AM

You can use StopCoroutine for this, However you will have to store the coroutine in order to stop it, because calling Regen will always return a new IEnumerator. So it isn't the same when u do StopCoroutine(Regen()); and it won't work. You will have to save and use it like so:

 IEnumerator couroutine = Regen();
 StopCoroutine(couroutine);

// Actual code:

     private Coroutine coroutine;
     void Update()
     {
         if (currentHealth > maxHealth)
         {
             currentHealth = maxHealth;
         }
         else if (currentHealth != maxHealth && !isRegen)
         { //If they have health less than max
             coroutine = Regen();
             StartCoroutine(coroutine); //Start regen
         }
     }
     //Function for taking damage
     public void TakeDamage(float damage)
     {
         if (isRegen)
         {
             StopCoroutine(couroutine);
             isRegen = !isRegen;
         }
         if (currentHealth <= 0)
         {
             Die();
         }
         else
         {
             currentHealth -= damage; //Remove health
         }
     }
Comment
Add comment · Show 3 · 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 fiterpilot · Aug 03, 2017 at 07:27 AM 0
Share

Thank you so much! I've been sitting here trying to figure this out for at least a good few hours. Finally have a solution lol!

avatar image ShadyProductions fiterpilot · Aug 03, 2017 at 07:34 AM 0
Share

Now it wasn't that hard now that you see the solution right?:P

avatar image fiterpilot ShadyProductions · Aug 03, 2017 at 08:24 AM 0
Share

Yeah. I looked at the stopcoroutine thing but didn't do it right so I figured it just didn't work

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

67 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 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 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 avatar image

Related Questions

Health Code, Damage and Healing 0 Answers

Health below zero but shows negative numbers 1 Answer

Health Bar going 0 right away 1 Answer

Help w/variables 0 Answers

Health/Damage system 1 Answer


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