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 chrisall76 · Jul 19, 2012 at 10:03 AM · timejumpregenerationhealth

Health Regeneration speeds up when action performed?

I have this health regeneration script, and that works fine. Except one problem where if I do something like jump health regenerates faster than it's suppose to. Here's the script:

 var healthRegenRate : float = 1;
 var hitPoints = 100;

 function Update(){
 Death();
 audio.clip = Heartbeat;

 DamagedTexture.active=false;
 //hitPoints = Mathf.Clamp(hitPoints + (healthRegenRate * Time.deltaTime), 0, 100);
 
 hitPoints += healthRegenRate * Time.deltaTime;
 
 if (Input.GetButtonDown("Fire2")){
     hitPoints -= 10;
 }
 if (hitPoints > 100){
     hitPoints = 100;
 }
 if (hitPoints <= 15){
    DamagedTexture.active = true;   
    if (!audio.isPlaying){
        audio.Play();
     } 
 }
 if (hitPoints >= 16){
    DamagedTexture.active = false; 
    if (audio.isPlaying){
        audio.Pause();
     }   
 }
 }

I think it has something to do with the Time.deltaTime, but I'm not to sure.

Comment
Add comment · Show 3
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 ByteSheep · Jul 19, 2012 at 11:03 AM 0
Share

I can't find anything wrong with this code, the line hitPoints += healthRegenRate * Time.deltaTime; should ensure that hitPoints is increased by one each second. In the script the healthRegenRate isn't being changed or anything so this script shouldn't be the problem.. Could you add the script in which you are calling the actions such as jump etc?

avatar image Mander · Jul 19, 2012 at 03:48 PM 0
Share

it could be Time.deltaTime remember that Time.deltaTime, as implied by the name, is simply the amount of time that has passed since the last frame.

edit: meant to type couldn't i hate english trololol thx anyways i think @merry_christmas explained a bit better

avatar image ByteSheep · Jul 19, 2012 at 04:39 PM 0
Share

Time.deltaTime is a useful way of making a variable always increase at the same rate, since you are adding the time that has passed since the last frame to that variable each time the Update() function is called.. This means that if you added Time.deltaTime to a variable, then after one second the variable would have a value of 1 (Frame rate doesn't matter). With this in $$anonymous$$d, Time.deltaTime can't be the reason that the healthRegenRate variable is increasing faster when the player jumps :)

1 Reply

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

Answer by tmiller_vb · Jul 19, 2012 at 07:37 PM

First try changing Time.deltaTime to Time.smoothDeltaTime. This'll smooth it out so that if your framerate takes a big hit (due to jumping, moving around, or other things that'd cause a lot of rendering).

If that doesn't work, try doing this in FixedUpdate() instead of Update(). FixedUpdate happens at a fixed rate, which seems to be more of what you want. With Update, if you have lots of Draw calls, this is going to slow it down, causing the time to be inconsistent. Additionally, change Time.deltaTime to Time.fixedDeltaTime so that it will be responding to the appropriate signal.

Comment
Add comment · Show 1 · 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 chrisall76 · Jul 20, 2012 at 02:33 AM 0
Share

Hopefully that works, but I can't tell because now my problem is that it regenerates too fast. And it only regenrates when I set HealthRegenRate to 50, and that's too fast.

 function FixedUpdate(){
 hitPoints += healthRegenRate *  Time.fixedDeltaTime; 
 if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E)){
     hitPoints -= 5;
     Debug.Log("HealthDown!");
 }
 if (hitPoints > 100){
     hitPoints = 100;
 }
 }

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can i make my health regenerate? 1 Answer

Health Regeneration Issues. 2 Answers

Increase health smoothly not instantly? 2 Answers

Health Regeneration 2 Answers

Creating a variable jump using deltaTime in C#. 0 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