• 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 himanshu619 · Dec 30, 2012 at 12:47 PM · healthbar

Health bar

Hi, I want to build a health bar with show's the energy of the player and when a particullar game-object with certain tag hit player then it decreases it's energy by certain points and when it totally decrease it then player die. And energy regain to certain level if some certain gameobject's (health booster) is collided with player.

My script is:

 var hitPoints = 100.0; 
 var detonationDelay = 0.0; 
 var explosion : GameObject; 
 var deadReplacement : Rigidbody; 
 
 function OnGUI()
 {
     GUI.HorizontalScrollbar(Rect (0,40,200,20), 0, hitPoints,0, 100);
 }
 
 function ApplyDamage (damage : float) { // We already have less than 0 hitpoints, maybe we got killed already? if (hitPoints <= 0.0) return;
 
 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 () { // Destroy ourselves Destroy(gameObject);
 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);
 }
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 Vonni · Jan 04, 2013 at 10:32 PM 0
Share

If you could format all code text with the 101011 button, that'd be great.

avatar image Hyperion · Jan 04, 2013 at 11:23 PM 0
Share

Hello, what's wrong with the script now?

avatar image himanshu619 · Jan 05, 2013 at 07:02 PM 0
Share

Script is kool, But, I want that the health bar to decrease when enemy hit player. Help me with updated code or give me idea!

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Davidovich · Jan 05, 2013 at 12:09 AM

At a glance it looks like the problem would be that the code is simply not being triggered. You would need to add one of two things:

  1. If it's the player colliding with the other object (such as walking over a med-pack) then the above script will need an OnCollisionEnter that calls the ApplyDamage function (you can call it with a negative value to regain health)

  2. If it's another object colliding with the player (such as a bullet) then that object will need an OnCollisionEnter function that calls ApplyDamage on the player. This is usually done via the SendMessage function.

If this doesn't help, you may want to add some more details about what exactly is not working and what the desired result is.

Comment
Add comment · Show 3 · 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 himanshu619 · Jan 05, 2013 at 07:04 PM 0
Share

Can you update it in code? Like when enemy with particular tag hit the "Player" with player tag then it automatically decrease the health bar with some value and when the health bar become zero then it destroy the player and give mgs on screen the "Game over".

avatar image AndyMartin458 · Jan 05, 2013 at 08:26 PM 0
Share

Yes. If you have a trigger or collider on your player, check the tag of the object in OnColliderEnter or OnTriggerEnter. Then you can do whatever you want with the player. If the object is doing the damage, then give the object a script. The player object can be retrieved in the trigger code and you can directly change the health or activate a function on the player that modifies the player's health (the latter is safer).

You need to decide on a methodology. 1. I want the player to detect a hit and do his own damage to himself. 2. I want the colliding object to detect the collision and call the functions on the player script.

avatar image himanshu619 · Jan 06, 2013 at 05:31 AM 0
Share

thanks, But I'm not getting the idea that how integrate enemy and player with health bar. Can you give me the supported code!

avatar image
0
Wiki

Answer by Hyperion · Jan 06, 2013 at 09:41 AM

Yeah, do what AndyMartin458 said. So overall, for the health booster version, here's the code. Remember to add a healthBooster tag to your health booster object by doing Edit->Project Settings->Tags->(click)Elements, size to however many tags you want to have in your game, (click)first empty element (example: Element01), type in tag. Also, click IsTrigger in the collider of the health booster object.

This code is for your player:

var HealthySound:AudioClip; //if you want to play sound upon collision

 function OnTriggerEnter(hit: Collider)
 {
 
        if (hit.gameObject.tag=="healthBooster")
           {
                  AudioSource.PlayClipAtPoint(HealthySound);
                  Destroy(hit.gameObject);  //destroys the health booster
                  function OnGUI();
                  function ApplyDamage ();
                  function DelayedDetonate();
                  // and so on with the functions       
           }
 }

--Put all your functions down here--

Hope this helps,

Hyperion

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 AndyMartin458 · Jan 06, 2013 at 09:04 PM 0
Share

Using this trigger code on your player follows methodology 1. By settings of Unity, that means the Unity inspector.

I would suggest not setting the tag in code (only check it as done in this code). In the Unity editor Select the game object that is the projectile and there is a section for GameObject and Tag. You might have to add a new tag (don't forget to click SaveProject after doing so).

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

Splitscreen w/ different GUIs on each camera 2 Answers

Healthbar not always working, someone please help! 2 Answers

[help] enemy Health Bar decrease at the same time 2 Answers

Creating A GUI health bar 0 Answers

OnGUI() not showing anything 2 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