Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 AusAndrew19 · Aug 01, 2013 at 06:49 AM · timedeltatimefunction updateregenerationhealth

Health Regeneration Issues.

I'm trying to implement a SLOW health regeneration system... That will regenerate 1 health per 1 second. This is what i have in my health script ATM.

 var regen : float = 1;
 
 function Update () {
  if(hitPoints < maxHitPoints) {
         hitPoints += regen * Time.deltaTime;
      }
 }

This code works but only when i put my regen variable above 47 and that is ridiculously fast. I have searched through answers.unity3d for a while but couldn't find a fix... Hopefully someone here knows how to fix this issue. Thanks for your time.

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

2 Replies

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

Answer by Griffo · Aug 01, 2013 at 06:55 AM

 var regen : float = 1;
 var loop : boolean;
 
 function Update () {
     if(hitPoints < maxHitPoints && !loop) {
         Health();
     }
 }
  
 function Health(){
 
     loop = true;
     yield WaitForSeconds (1); 
     hitPoints ++;
     loop = false;
 }


Try this ..

Comment
Add comment · Show 6 · 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 AusAndrew19 · Aug 01, 2013 at 06:59 AM 0
Share

Its smoother, But regenerates almost instantly.

avatar image Griffo · Aug 01, 2013 at 08:16 AM 0
Share

Ok .. it works now ..

avatar image AusAndrew19 · Aug 01, 2013 at 09:02 AM 0
Share

No it doesn't... Same thing as your first post.. Its smooth but makes health restore instantly.

avatar image Griffo · Aug 01, 2013 at 09:29 AM 0
Share

Yes it does .. Ive tested it on an empty GameObject, I set maxHitPoints in the inspector to 20 then watched hitPoints increase from 0 by 1 every second until it reached 20 in the inspector ..

 var regen : float = 1;
 var loop : boolean;
 var hitPoints : float;
 var maxHitPoints : float;
  
 function Update () {
     if(hitPoints < maxHitPoints && !loop) {
         Health();
     }
 }
  
 function Health(){
  
     loop = true;
     yield WaitForSeconds (1); 
     hitPoints ++;
     loop = false;
 }
avatar image AusAndrew19 · Aug 01, 2013 at 09:36 AM 0
Share

Okay Buddy... I just tryed the code you posted and yes it works... The problem i had i didn't see && !loop)

Now This Works 100% and i can give u +1 and accept answer, Thanks for your time and i really appreciate it :) Thank you!

Show more comments
avatar image
0

Answer by Shar1ngan · Aug 01, 2013 at 07:08 AM

 var timer : float = 0;
 var regenPerSecond : float = 1;

 function Update () {
  timer += Time.deltaTime;
 
  if(hitPoints < maxHitPoints) 
  {
     if(timer >= 1)
        hitPoints += regenPerSecond;
        timer = 0;
      }
  }
 }

or try this (without regen)

 function Update () {
  if(hitPoints < maxHitPoints) {
        hitPoints += Time.deltaTime;
      }
 }

Sum of Time.deltaTime in per second = 1 as i know...

Comment
Add comment · Show 5 · 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 AusAndrew19 · Aug 01, 2013 at 07:23 AM 0
Share

Neither of those worked...

avatar image Shar1ngan · Aug 01, 2013 at 07:31 AM 0
Share

and how it work?

avatar image AusAndrew19 · Aug 01, 2013 at 07:33 AM 0
Share

@Share1ngan, No idea what you mean... Not One Of The Above Worked.

avatar image Shar1ngan · Aug 01, 2013 at 07:36 AM 0
Share

Probably because maxHitPoints is not a percentage of the total XP

if you have 2000 hp, with this code, it will recover very slowly. (or if maxHitPoints = 10, it will recover very fast)

Try set in Start or other function (where you set maxHitPoints value)

 regenPerSecond = maxHitPoints / 100; 

then will regenerate 1% per second with first code.

avatar image AusAndrew19 · Aug 01, 2013 at 09:10 AM 0
Share

When the timer hits 0. The timer doesn't reCount so it only gives 1 health ever..

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

17 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

Related Questions

Health Regeneration speeds up when action performed? 1 Answer

Add 1 Per Second to an Int 1 Answer

How can i make my health regenerate? 1 Answer

how to change the filedof view slowly 2 Answers

Execute code every x seconds with Update() 4 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