• 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 · Apr 19, 2013 at 10:26 AM · multiplayerfps

Bleeding and Health Script Problem

Hey guys, I got a little problem :) In this script I was trying to create a little health system and the regeneration works great so far :) But the bleeding, not so good :/ Could someone give me a hint on what I can do to make both work the way I want to? Thank you in advance!

var MaxHealth = 80.0;

 var health = MaxHealth;
  
 //Time Till the Player starts to Regenerate after timer Trigger
 var regenTime = 10.0;
 //How fast the Player Regenerates
 var regenSpeed = 1.0;
  
 //Time till the player starts to Bleed after timer Trigger
 var bleedTime = 50.0;
 //How fast the Player Bleeds
 var bleedSpeed = 1.0;
  
 //Timer for Regeneration/Bleeding
 private var timer = 0.0;
  
 function Start () {
  
 }
  
 function Update () {
 
     if(health<=0)
        die();
  
     if(health>20 && health<MaxHealth){
        if(timer<regenTime) {
          timer+=0.1;
        }else{
          health += regenSpeed/10;
        }
        }else{
          timer = 0.0;
      
     if(health<=20 && health>0){
        if(timer<bleedTime) {
          timer+=0.1;
        }else{
          health -= bleedSpeed/10; 
        }
      }else
          timer = 0.0;  
     }
 } 
 function die () {
 }
Comment
Add comment · Show 2
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 Fattie · Apr 19, 2013 at 10:26 AM 0
Share

for trivial timers in Unity, use Invoke() or InvokeRepeating(). unityGE$$anonymous$$S.com has many articles on beginner timers in Unity. hope it helps

avatar image Borzi · Apr 19, 2013 at 09:53 PM 0
Share

Okay will look at it! Thanks :)

1 Reply

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

Answer by stevedh · Apr 19, 2013 at 04:40 PM

just a few comments. Ideally you should add Time.deltaTime to timer instead of 0.1 so that bleedtime doesn't vary for different framerates.

you're using the same timer variable for both regen and health, but regen and health are treating it as if they own it, i.e. both are incrementing and zeroing it causing conflicts.

I'd suggest adding a breakpoint and single stepping so that you can see whats really happening, or at least outputting some values to the console

have fun Steve

ok Looks like you can't have multiple comments (I'm new to this) I'd possibly do something like below, although I'm not sure of the overall aim, but maybe that's because this is only a fragment of the whole code or are you destined to bleed to death as soon as you're health drops below 20

anyway code is untested and uncompiled so you may need to tweek it a bit.

 Time.deltaTime
 
 
 var MaxHealth = 80.0;
 var BleedThreshold = 20.0;
 var RegenThreshold = 20.0;
 
 var health = MaxHealth;
  
 //Time Till the Player starts to Regenerate after timer Trigger
 var regenTime = 10.0;
 //How fast the Player Regenerates
 var regenSpeed = 1.0;
  
 //Time till the player starts to Bleed after timer Trigger
 var bleedTime = 50.0;
 //How fast the Player Bleeds
 var bleedSpeed = 1.0;
  
 //Timer for Regeneration/Bleeding
 private var RegenTimer = 0.0;
 private var BleedTimer = 0.0;
 
 function Start () {
     RegenTimer = regenTime;
     BleedTimer = bleedTime;
 }
  
 function Update () {
  
     if(health<=0)
        die();
  
     if(health>RegenThreshold && health<MaxHealth){
        if(RegenTimer>0.0) {
      RegenTimer-=Time.deltaTime;
        }else{
          health += regenSpeed*Time.deltaTime;
        }
        }else{
         RegenTimer = regenTime;
  
     if(health<=BleedThreshold && health>0){
        if(BleedTimer>0.0) {
          BleedTimer-=Time.deltaTime;
        }else{
          health -= bleedSpeed*Time.deltaTime; 
        }
      }else
         BleedTimer = bleedTime;  
     }
 } 
 function die () {
 }



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 Borzi · Apr 19, 2013 at 09:33 PM 0
Share

Okidoki, thank you so much for the tipps and especially for the effort of making a skript for me :) I managed to fix it pretty easily, all I did was actually add another timer that took care of the bleed time and one for health regeneration :D It works perfectly for my purposes although its a pretty simple skript really:

 #pragma strict
 var $$anonymous$$axHealth = 80.0;
  
 var health = $$anonymous$$axHealth;
  
 //Time Till the Player starts to Regenerate after timer Trigger
 var regenTime = 10.0;
 //How fast the Player Regenerates
 var regenSpeed = 1.0;
  
 //Time till the player starts to Bleed after timer Trigger
 var bleedTime = 50.0;
 //How fast the Player Bleeds
 var bleedSpeed = 1.0;
  
 //Timer for Regeneration/Bleeding
 private var timer1 = 0.0;
 private var timer2 = 0.0;
  
 function Start () {
  
 }
  
 function Update () {
 
     if(health<=0)
        die();
  
     if(health>20 && health<$$anonymous$$axHealth){
        if(timer1<regenTime) {
          timer1+=Time.deltaTime;
        }else{
          health += regenSpeed/10;
        }
        }else{
          timer1 = 0.0;
      
     if(health<=20 && health>0){
        if(timer2<bleedTime) {
          timer2+=Time.deltaTime;
        }else{
          health -= bleedSpeed/20; 
        }
        }else
          timer2 = 0.0;  
     }
 } 
 function die () {
 }

Hahaha wish I could thumbs you up but unfortunately I cant :D Cheers mate!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

12 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

Related Questions

Multiplayer FPS Tutorial 1 Answer

When a host player changes his weapon all players get switched to it 1 Answer

Making a fps online game 3 Answers

How to hide an object from other clients using PUN 2 0 Answers

Setting up a Character Question. 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