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 BrandMusic · Jan 14, 2014 at 10:05 PM · beginnerregeneration

How do I regenerate some health each second?

So I know this has been asked before and I've tried a lot of online solutions already. I've written this about 10 times 10 different ways and I simply can't stand that I'm still getting errors. I may not be using Time.deltaTime right since I've never used it for anything before. Code is below, any help is appreciated.

alt text

healthprob.png (22.1 kB)
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
0
Best Answer

Answer by Josh707 · Jan 15, 2014 at 12:11 AM

You can't do operations like on lines 11 & 12 when they're outside of any functions, so you will have to put them somewhere else like Update if you want it to calculate every frame.

Multiplying second by deltaTime will cause it to constantly decrease until it's 0, at least if you're running faster than 1FPS. It's the time in seconds the last frame took took to complete everything - so it will be a pretty small decimal at high frame rates. In your case I would increment the variable by deltaTime and if it is higher than your limit variable reset it to 0 and do your regeneration stuff.

 var regeneration : float = 5.0;
 var regenerationRate : float; //time in seconds
 private var second : float = 0.0;
 
 function Heal(){
     if(curHealth < maxHealth){
         second += Time.deltaTime;
         if(second >= regenerationRate){
             curHealth = Mathf.Min(curHealth + regeneration, maxHealth);
             second = 0;
         }
     }
 }

If you want it to be each second then setting the rate to 1.0 should be pretty close to a second, but if you want it to constantly increase then you can just directly increase the health with a value multiplied by deltaTime so it raises consistently.

 curHealth = Mathf.Min(curHealth + (someAmount * Time.deltaTime), maxHealth);

 
Comment
Add comment · Show 2 · 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 BrandMusic · Jan 15, 2014 at 05:40 PM 0
Share

having some trouble analyzing your code. shouldn't mathf.max just keep returning maxHealth since it will always be the greater of the two numbers? (you know unless they're equal)

avatar image Josh707 · Jan 16, 2014 at 01:01 AM 0
Share

Oh whoops, that was supposed to be $$anonymous$$athf.$$anonymous$$in. I've changed it now!

avatar image
0

Answer by Jinxology · Jan 15, 2014 at 12:15 AM

You need to put:

 if (healthy) Heal();
 

inside the Update function:

 function Update() {
    if (health) Heal();
 }

so that it runs every frame. Think of your regeneration rate as the amount healed in 1 second, then simply multiply that by Time.deltaTime (the number of seconds since the last frame).

Think of it this way: If you want to heal 10 HP a second, and the time since the last update (deltaTime) is only .5, then you would only want to heal for 5 HP.

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

20 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

Related Questions

Is there any difference if I duplicate a prefab in Hierarchy or drag it from Project tab? 1 Answer

Scene view doesn't appear to be working 4 Answers

Help with SpringFollowCamera 0 Answers

What are the best tutorial videos to learn just Javascript? 1 Answer

Beginner question about GUI scripting 2 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