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 Borzi · Aug 02, 2013 at 09:16 AM · javascriptfunctionintregeneration

Stopping Regen from Overhealing?

Hey, I am working on a regeneration function where the players health goes back to 100 when he has collected a power up. The maximum Health that the player can attain is 200.

 function Regenerate ()
 {
 //Check if the Regeneration has set in
 if(regenTime >= RegenTimerDuration)
     {
     //If not, start the Timer 
     RegenTimerDuration += 1 * Time.deltaTime;
     }
 //If the player has lost some Health
 if(health < InitialHealth)
     {
         //If the timer is bigger then the Regeneration Rate,
         //Start with the Regeneration process
         if(regenTimer <= regenerationRate)
         {    
             //If the first timer has set off
             if(regenTime <= RegenTimerDuration)
             { 
             //Start counting up to surpass the regenRate
             regenTimer += 1 * Time.deltaTime;
             }             
         }
     }
 //If the Timer is Bigger
 if(regenTimer >= regenerationRate)
     {
     //Reset it
     regenTimer = 0;
     //Add to "health"
     health += regenSpeed; 
     }
 //If the Regeneration is False
 if(regeneration == false)
     {
     //Reset the Timer
     regenTimer = 0;
     //And the Time to Regenerate
     RegenTimerDuration = 0;
     }
 }

The problem: If health = 98 and regeneration is active, it will still add 4 (or whatever number regenSpeed is equal to). Can someone tell me how I would limit it to adding exactly the right amount?

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
1
Best Answer

Answer by CHPedersen · Aug 02, 2013 at 09:23 AM

You can use clamping to clamp it to 100, for example:

 health = Mathf.Clamp(health + regenSpeed, 0, 100);
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 Borzi · Aug 02, 2013 at 09:56 AM 0
Share

Would I have to place that anywhere specific? Or just in the function

avatar image CHPedersen · Aug 02, 2013 at 10:02 AM 0
Share

I would put it in any position where the health variable is getting modified through the regenSpeed, e.g. this line here:

health += regenSpeed;

avatar image Borzi · Aug 02, 2013 at 10:05 AM 0
Share

Like so (pretend the $$anonymous$$yPlayer. isnt there)?

     //If the Timer is Bigger
     if($$anonymous$$yPlayer.regenTimer >= regenerationRate)
     {
     //Reset it
     $$anonymous$$yPlayer.regenTimer = 0;
     //Add to "Health"
     $$anonymous$$yPlayer.health += regenSpeed;
     $$anonymous$$yPlayer.health = $$anonymous$$athf.Clamp($$anonymous$$yPlayer.health + regenSpeed, 0, 100);
     }

avatar image CHPedersen · Aug 02, 2013 at 10:34 AM 1
Share

No. The above would probably work, but you're still unnecessarily incrementing health before the clamp. This:

 $$anonymous$$yPlayer.health += regenSpeed;

Is shorthand for writing:

 $$anonymous$$yPlayer.health = $$anonymous$$yPlayer.health + regenSpeed;

Which, in words, says "Set the health variable of the $$anonymous$$yPlayer object to its own value, plus the value of the variable regenSpeed". You don't want to do that. You want it to say, "Set the health variable of the $$anonymous$$yPlayer object to itself plus the value of regenSpeed, but keep the value between 0 and 100". The clamping does that.

In short: Remove this line

 $$anonymous$$yPlayer.health += regenSpeed;

Because it does the same as the line that uses clamp, but without clamping.

avatar image Borzi · Aug 02, 2013 at 11:21 AM 0
Share

Great thank you very much :)

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

15 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

Related Questions

Problem accessing variable in javascript 1 Answer

Accessing function from another script won't work 1 Answer

int.parse with decimal? 1 Answer

How to pass a int from a object to another 1 Answer

running code outside of update! 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