Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 john-essy · Aug 14, 2011 at 10:50 AM · health

Health Regeneration

Hi there and thanks for looking. The problem i am having is this. When i hit an object i want my script to wait 10 seconds then start regenerating health but if i hit something again within those 10 seconds restart the timer before regenerating health.

As when i hit something within the ten seconds it adds health after 10 seconds for the first object hit then does the same for the second. here is the full script

 var health : int = 100;
 var lives : int;
 var levelNameToLoadWhenDead : String;
 var levelNumToLoadWhenDead : int;
 var easyMode : boolean = false;
 var normalMode : boolean = false;
 var hardMode : boolean = false;     ///////// delete all the min health and max health apart from 1 each as this is all we need
 var realisticMode : boolean = false;
 var healthRegenaration : boolean = false;
 var healthRegenerationTime : int = 10; // Time it takes to start regenerating health
 var loseHealthTag1_5health : String;// In the inspector just type the name of the Tag you want to be affected by // TRY AN ARRAY IN HERE [] !!!!!!!!!!!!!!!!!!!!!!!
 var loseHealthTag2_10health : String;
 var loseHealthTag3_15health : String;
 var loseHealthTag4_20health : String;
 var loseHealthTag5_25health : String;// In the inspector just type the name of the Tag you want to be affected by
 var loseHealthTag6_30health : String;
 var loseHealthTag7_35health : String;
 var loseHealthTag8_40health : String;
 var loseHealthTag9_45health : String;// In the inspector just type the name of the Tag you want to be affected by
 var loseHealthTag10_50health : String;
 
 @HideInInspector var gotHit : boolean = false;
 @HideInInspector var easyHealth : int = 200;
 @HideInInspector var normalHealth : int = 100;
 @HideInInspector var hardHealth : int = 75;
 @HideInInspector var realisticHealth : int = 50;
 @HideInInspector var checkpoint1 : boolean = false;
 @HideInInspector var checkpoint2 : boolean = false;
 
 
 function Start()
     {
         Difficulty();
     }
 
 
 function Update()
 {
     Modes();
     Health();
 
 }
 
 
 
 // EasyMode   ///////////  EasyMode /////////// EasyMode
 function OnTriggerEnter(hit : Collider)
 {
 
         if(hit.gameObject.tag == loseHealthTag1_5health && easyMode == true)
         {
             health -= 5;
             Debug.Log(health);
                 if(healthRegenaration == true)
                     {
                         gotHit = true;
                         HealthRegenaration();
                         gotHit = false;
                     }
             
         }
 
         
         if(hit.gameObject.tag == loseHealthTag2_10health && easyMode == true)
         {
             health -= 10;
             Debug.Log(health);
                 if(healthRegenaration == true)
                     {
                         gotHit = true;
                         HealthRegenaration();
                         gotHit = false;
                     }
         }
     
     
         if(hit.gameObject.tag == loseHealthTag3_15health && easyMode == true)
         {
             health -= 15;
             Debug.Log(health);
                 if(healthRegenaration == true)
                     {
                         gotHit = true;
                         HealthRegenaration();
                         gotHit = false;
                     }
         }
     
     
         if(hit.gameObject.tag == loseHealthTag4_20health && easyMode == true)
         {
             health -= 20;
             Debug.Log(health);
                 if(healthRegenaration == true)
                     {
                         gotHit = true;
                         HealthRegenaration();
                         gotHit = false;
                     }        
         }
     
     
         
     
     if(hit.gameObject.tag == loseHealthTag1_5health && normalMode == true)
         {
             health -= 10;
             Debug.Log(health);
                 if(healthRegenaration == true)
                     {
                         gotHit = true;
                         HealthRegenaration();
                         gotHit = false;
                     }
             
         }
 
         
         if(hit.gameObject.tag == loseHealthTag2_10health && normalMode == true)
         {
             health -= 20;
             Debug.Log(health);
                 if(healthRegenaration == true)
                     {
                         gotHit = true;
                         HealthRegenaration();
                         gotHit = false;
                     }
         }
     
     
         if(hit.gameObject.tag == loseHealthTag3_15health && normalMode == true)
         {
             health -= 25;
             Debug.Log(health);
                 if(healthRegenaration == true)
                     {
                         gotHit = true;
                         HealthRegenaration();
                         gotHit = false;
                     }
         }
     
     
         if(hit.gameObject.tag == loseHealthTag4_20health && normalMode == true)
         {
             health -= 50;
             Debug.Log(health);
                 if(healthRegenaration == true)
                     {
                         gotHit = true;
                         HealthRegenaration();
                         gotHit = false;
                     }        
         }
         
         
             if(hit.gameObject.tag == loseHealthTag1_5health && hardMode  == true)
         {
             health -= 20;
             Debug.Log(health);
                 if(healthRegenaration == true)
                     {
                         gotHit = true;
                         HealthRegenaration();
                         gotHit = false;
                     }
             
         }
 
         
         if(hit.gameObject.tag == loseHealthTag2_10health && hardMode  == true)
         {
             health -= 25;
             Debug.Log(health);
                 if(healthRegenaration == true)
                     {
                         gotHit = true;
                         HealthRegenaration();
                         gotHit = false;
                     }
         }
     
     
         if(hit.gameObject.tag == loseHealthTag3_15health && hardMode == true)
         {
             health -= 30;
             Debug.Log(health);
                 if(healthRegenaration == true)
                     {
                         gotHit = true;
                         HealthRegenaration();
                         gotHit = false;
                     }
         }
     
     
         if(hit.gameObject.tag == loseHealthTag4_20health && hardMode  == true)
         {
             health -= 50;
             Debug.Log(health);
                 if(healthRegenaration == true)
                     {
                         gotHit = true;
                         HealthRegenaration();
                         gotHit = false;
                     }        
         }
         
             if(hit.gameObject.tag ==  loseHealthTag1_5health && realisticMode  == true)
         {
             health -= 50;
             Debug.Log(health);
                 if(healthRegenaration == true)
                     {
                         gotHit = true;
                         HealthRegenaration();
                         gotHit = false;
                     }        
         }
         
         if(hit.gameObject.name == "CheckPoint")
             {
                 checkpoint1 = true;
                 checkpoint2 = false;
                 Destroy(hit.gameObject);
             }
             
         if(hit.gameObject.name == "CheckPoint2")
             {
                 checkpoint2 = true;
                 checkpoint1 = false;
                 Destroy(hit.gameObject);
                 Debug.Log(checkpoint1 && checkpoint2);
             }
     
     
         
         // We neeed to add a GUI function to keep track of health
         
 
         
         
 }
 
 
 
 
 function Modes() // For the Editor
 {
     if(easyMode)
         {
             normalMode = false;
             hardMode = false;
             realisticMode = false;
         }
         
     if(normalMode)
         {
             easyMode = false;
             hardMode = false;
             realisticMode = false;
         }
         
     if(hardMode)
     
         {
             easyMode = false;
             normalMode = false;
             realisticMode = false;
         }
         
     if(realisticMode)
         {
             easyMode = false;
             normalMode = false;
             hardMode = false;
         }
     
 
 
 }
 
 
 function HealthRegenaration()
 {
         
         
         if(gotHit == true && healthRegenaration == true);
         
             yield WaitForSeconds (healthRegenerationTime);
             Debug.Log(healthRegenerationTime);
             health += 10;
             // Here i need to get it so if we hit an object start a timer counting down from ten then take health away if we hit something again within the 10 seconds restart the timer before health is debducted 
             
             
         
     
  
         
 }
 
 function Health()
 {
     if(health <= 0)
         {
             lives -= 1;
             if(checkpoint1 == false && checkpoint2 == false)
                 {
                     transform.position = Vector3 (1,5,1);
                     health = 100;
                 }
             
             
             if(checkpoint1 == true)
                 {
                     transform.position = Vector3 (25,10,25);// In here type the Co-ordinates of where you would like to spawn. When you die
                     health = 100;
                 }
             
             if(checkpoint2 == true)
                 {
                     transform.position = Vector3 (10,20,10);
                     health = 100;
                 }
         }
         
 
         /*
     if(lives <= 0)
         {
             Application.LoadLevel(levelNumToLoadWhenDead || levelNameToLoadWhenDead);
         }
         */
         
     if(easyMode == true && health >= easyHealth)
         {
             health = easyHealth;
         }
         
     if(normalMode == true && health >= normalHealth)
     
         {
             health = normalHealth;
         }
         
     if(hardMode == true && health >= hardHealth)
         {
             health = hardHealth;
         }
         
     if(realisticMode == true && health >= realisticHealth)
         {
             health = realisticHealth;
         }
         
 
 }
 
 
 function Difficulty()
 {
     if(easyMode == true)
         
         {
             health = easyHealth;
         }
     
     if(normalMode == true)
         
         {
             health = normalHealth;
         }
         
     if(hardMode == true)
         
         {
             health = hardHealth;
         }
         
     if(realisticMode == true)
         {
             health = realisticHealth;
         }
 }
 
 
 function OnGUI ()
     {
         if(checkpoint1 == true)
             {
                 GUI.Label (Rect (25, 25, 100, 60), "You reached the Checkpoint");
             }
             
         if(checkpoint2 == true)
             {
                 GUI.Label (Rect(25,25,100,60), "You reached another Checkpoint Your smart!");
             }
     }
 
 
     
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
3

Answer by SilverTabby · Aug 14, 2011 at 12:45 PM

This part of your script is the problem:

 function HealthRegenaration()
 {

     if(gotHit == true && healthRegenaration == true);

         yield WaitForSeconds (healthRegenerationTime);
         Debug.Log(healthRegenerationTime);
         health += 10;
 }

one, the if statement does nothing: the semicolon kills it so there is nothing inside of it

two, more importantly, you are just creating a coroutine that waits a bit and regens. This is far from an optimal approach. I would do the following:

  • kill this function. It is causing more harm than good.

  • create a var at the top of the script. Call it "timeOfLastHit" or something along those lines. In it, store a float. Every time you hit an object, get hurt, etc. do this:

    timeOfLastHit = Time.time;

  • Then, in the Update function,

    if(Time.time - timeOfLastHit > healthRegenerationTime)

      //do health regen code here.
    
    
    

I hope this has helped point you in the right direction.

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 Waz · Aug 14, 2011 at 01:03 PM 1
Share

Looks like we got ourselves a Reader.

Well done, you're very patient.

avatar image john-essy · Aug 14, 2011 at 04:26 PM 0
Share

Thank you soooo much will try that now

avatar image SilverTabby · Aug 14, 2011 at 08:35 PM 0
Share

Honestly, I just glanced at every fourth block of code and stopped when I saw the health regen function :P

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How can i make my health regenerate? 1 Answer

Error when player trying to take damage from a hazard 0 Answers

Health not working 1 Answer

Damage/Healing over time 1 Answer

Player take damage on collision with AI 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