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 FrostHDD · Oct 29, 2012 at 12:18 AM · playermmohealth

health bar script please help

hello im having trouble making the player die when his health reaches 0 this is my script just need help with the die part here is my script:

 var currentHealth = 100;
 var maxHealth = 100;
 var minHealth = 1;
 
 if(currentHealth <= minHealth);
 currentHealth = 0;
 
 if(currentHealth >= maxHealth); 
 currentHealth = 100;
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

Answer by Montraydavis · Oct 29, 2012 at 12:41 AM

 //Code detect death
 
 var health : float = 100 ;
 function Update ( )
 {
     if (  Mathf.Ceil ( health ) == 0 )
     {
         PlayerIsDead . . . . 
     }else{
         health -= 1 * Time.deltaTime ;
     }
 }
Comment
Add comment · Show 4 · 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 podperson · Oct 29, 2012 at 12:48 AM 0
Share

Um what? So you'll die in 100s no matter what?

Also -- don't use update events if you can help it. Used FixedUpdate -- less overhead. For something like this you could even thread (coroutine) the code and only wake up every 0.1s or so and have even less overhead.

avatar image Montraydavis · Oct 29, 2012 at 02:23 AM 0
Share

I understand. This was somewhat a starting point, or example more than a solution . Thanks for the suggestion as well. I will keep that in $$anonymous$$d.

avatar image FrostHDD · Oct 31, 2012 at 12:39 PM 0
Share

thank you everyone it very helpful

avatar image Montraydavis · Oct 31, 2012 at 02:20 PM 0
Share

You are welcome! If you do not $$anonymous$$d, mark the answer as answered, and a thumbs up ;) . Thanks for supporting Unity.

avatar image
0

Answer by podperson · Oct 29, 2012 at 12:53 AM

Simple solution:

 function FixedUpdate(){
   if( currentHealth <= minHealth ){
     // player is dead -- careful this doesn't get called over and over!
   }
 }

Here's a coroutine solution. You call CheckIfDead() once and it sits in the background and checks every so often.

 function Start(){
   ...
   CheckIfDead();
 }
 
 function CheckIfDead(){
   while( currentHealth > minHealth ){
     yield WaitForSeconds(0.1); // check to see if player has died ever 0.1s
   }
   // this code executes when the character is dead
 }
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

11 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

Related Questions

Multiple Cars not working 1 Answer

How to make a player die after health reaches 0 3 Answers

Add spawn to script [Multiplayer] 0 Answers

Life Player loads another level 1 Answer

How can I load a level when health depletes to 0? 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