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 DayyanSisson · Aug 28, 2011 at 10:39 PM · timedamagereceiverregeneratehealth

Health Regeneration

I have a damage receiver script and in it, I wrote a script to regenerate health:

 #pragma strict
 
 var hitPoints = 100.0;
 var regen = 10; 
 var detonationDelay = 0.0; 
 var explosion : GameObject; 
 var deadReplacement : Rigidbody;
 
 function Update() {
     if(hitPoints <= 100) {
         hitPoints += regen * Time.deltaTime;
     }
 }

The function update of course being the regeneration part. Well it's not working. Here's the whole script:

 #pragma strict
 
 var hitPoints = 100.0;
 var regen = 10; 
 var detonationDelay = 0.0; 
 var explosion : GameObject; 
 var deadReplacement : Rigidbody;
 
 function Update() {
     if(hitPoints <= 100) {
         hitPoints += regen * Time.deltaTime;
     }
 }
 
 function ApplyDamage (damage : float) { 
 
 hitPoints -= damage;
 if (hitPoints <= 0.0) {
    
     var emitter : ParticleEmitter = GetComponentInChildren(ParticleEmitter);
     if (emitter)
         emitter.emit = true;
 
     Invoke("DelayedDetonate", detonationDelay);
 }
 }
 
 function DelayedDetonate () { BroadcastMessage ("Detonate"); }
 
 function Detonate () {
      
 if (explosion)
     Instantiate (explosion, transform.position, transform.rotation);
 
 
 if (deadReplacement) {
     var dead : Rigidbody = Instantiate(deadReplacement, transform.position, transform.rotation);
 
     
     dead.rigidbody.velocity = rigidbody.velocity;
     dead.angularVelocity = rigidbody.angularVelocity;
 }
 
 
 var emitter : ParticleEmitter = GetComponentInChildren(ParticleEmitter);
 if (emitter) {
     emitter.emit = false;
     emitter.transform.parent = null;
 }
 Destroy (gameObject);
 }

I'm not sure why it's not working. The regen should be adding to it every second by ten, but, nothing is happening. Why is this?

Comment
Add comment · Show 4
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 28, 2011 at 10:48 PM 0
Share

For performance, regen should be float (10.0), but that shouldn't cause this problem, since multiplying by float should convert it.

Add some debugs. Is Update being called? (won't be if you subclass and don't call super.Update)

avatar image DayyanSisson · Aug 28, 2011 at 10:58 PM 0
Share

I'm still new to scripting and I pretty much skipped debugging when I was learning so I'm not sure how to debug. Oh, and Update is being called because the script that is up is not the whole script, and there are some other things in Update which are working.

avatar image Waz · Aug 28, 2011 at 11:01 PM 0
Share

Just print values to debug, I.e. put in the if:

 Debug.Log("value was "+hitPoints);
avatar image DayyanSisson · Aug 28, 2011 at 11:08 PM 0
Share

Okay, I'm gonna put this in the question but, I just realized the script only regenerates when I'm below a certain amount of health. That's when it starts regenerating, and when it gets to the same amount, it stops regenerating, it doesn't regenerate. Then I realized that it only regenerates if it's below 100. Well the player has 500 health. So I changed the script. It works now.

Oh, and thank you for the debugging. I'll look into it now. Oh, and you should post an answer so I can say it's correct.

2 Replies

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

Answer by DayyanSisson · Aug 30, 2011 at 12:04 AM

Okay, since the question has been answered by myself (look at the comments of the question) I'm just posting this to say it's correct. You are free to use the script as you like.

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
avatar image
0

Answer by AngryMarine · Aug 29, 2011 at 02:25 AM

This is how I calculate regeneration. This uses 3 variables (all floats) but only 1 is changed so it really doesn't impact performance. Also, I pair this with a death check formatted very similar that uses a <= 0 check in the same IF so that I don't get regen/death check conflicts.

if ( structuralIntegrity < maxStructuralIntegrity ) {

     // Death Check goes here with a return(); to prevent further regen
       structuralIntegrity += integrityRegen * Time.deltaTime;

}

Hope this helps!

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 DayyanSisson · Aug 29, 2011 at 04:04 AM 0
Share

Thanks, but if you read the comments, I already answered the question. Thanks a lot though!

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

regenerating health 3 Answers

Increase health smoothly not instantly? 2 Answers

Health Question 1 Answer

Damage script 0 Answers

From script A in script B 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