• 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
Question by Ekta-Mehta-D · Aug 12, 2013 at 12:43 PM · enemyhudhealthbarhealth

How to create Enemy Health Bar ?

Hello everyone ,

I am new to unity.. I want to develop enemy Health bar / Life bar.. My enemy's total health value is 400. Whenever player shoot enemy ,and i am decreasing health value..

Same time i want to update the life bar of enemy..

For that i have make script which i have assigned to main camera :

 #pragma strict
 
     public var  barIdle : Texture2D;
     public var  barFull : Texture2D;
     public var scores : int;
     
     var  initialWidth : float;
     var  barRect : Rect;
     var  score : GameObject;
     var  scoreScript : ScoreController;
 
 function Start () {
     scoreScript = GameObject.Find("Main Camera").GetComponent("ScoreController");    
 }
 
 function Update () {
     barRect = new Rect(20, 10, 205, 25);
     
 }
 
 function OnGUI()
 {
     if(scoreScript.onceLargeEnemy)// when my enemy is instantiated
     {
         GUI.DrawTexture(barRect, barIdle);
         GUI.BeginGroup(new Rect(barRect.x, barRect.y, (initialWidth - (initialWidth/40)), barRect.height));
         GUI.DrawTexture(new Rect(0, 0, barRect.width, 15), barFull);
         GUI.EndGroup();
     }
 }

But i failed to perform this task.. so please guide me and help me out to implement health bar..

Thank you in advance..

Comment

People who like this

0 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 noooodley · Aug 12, 2013 at 06:57 PM 0
Share

You could check out NGUI. It makes doing anything UI related much simpler.

avatar image moghes · Aug 29, 2013 at 01:34 PM 0
Share

@Ekta Mehta D. Its been weeks that you asked and unfortunately no one answered your answer correctly..

First where you are updating your 'initialWidth' variable?

Or have you considered the other way? You can have an array of 6-10 textures of the helthbar and display the proper one dynamically.. It might not be the optimal way but easy and gets your job done.

4 Replies

· Add your reply
  • Sort: 
avatar image

Answer by VIPINSIRWANI · Aug 28, 2013 at 06:56 AM

Hey...Follow some steps 1. you need to take two images to show health bar. 2. Both put at same position. 3. now upper imager suppose its color is blue you just need to decrease the size x scale of upper image like on collision enter or whatever you are using. 4. Now back image suppose its color is red its looks like health bar with the combination of both images.

Comment

People who like this

0 Show 0 · 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

Answer by Face_Mcgace · Aug 29, 2013 at 01:54 PM

If I was you I wouldn't use Unity's built in OnGUI -- go grab NGUI and follow this post http://www.tasharen.com/forum/index.php?topic=130.0 if you want an easy time with Health Bars.

Comment

People who like this

0 Show 0 · 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

Answer by Java666 · Jun 05, 2014 at 08:28 PM

i disagree with statement ngui makes things simpler i been using ngui for last 2 months now and most things i can get done in unity's gui in 10min to an hour i'm doing an internship and trying to use ngui because i have to and most things have taken 5-6 days just to get same effects done as using unity's gui, i might be new to programing but to me if i can do it in unity gui faster than ngui and its meant to be faster i dont see how. in a few things maybe faster but not all and still cant get somethings working right or at all with ngui yet can with unity gui in like an hour tops for the hard things.

not sure why the health bar would be on the camera, i create a script similar to this one but have it on the enemy so that i can connect it to the enemies health easier.

 private float currentHealth;
 private float maxHealth;

 private float normalisedHealth;

which is like yours sorry but i use c# not javascript not to hard to convert tho, i use an public Texture2D bgImage;

like you i did try 2 images but found the one that moves doesnt work the way it should even with right coding done so i only use the one image and create a rect that fits on top of the other one for moving it around and just made it red.

 private Characters script;

thats the one with the enemies or players stat info on it. in the start function i connect it like so

script = GetComponent();

for whatever reason only way i could get it to work right was to put the connection to the stats in the OnGui function

     currentHealth = script.currentHealth;
     maxHealth = script.maxHealth;

normalisedHealth = ((float)currentHealth / maxHealth) * 100; the 100 is used because it my health bars width if ours is not 100 then use your width that you set up in your Rect.

thats to make it so that the health bar that moves is right size at all times

than the rest of my health bar is done like your is.

if you use OnGui it places on the camera view area for you no need to have it connected to the camera on placed on it.

Comment

People who like this

0 Show 0 · 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

Answer by Jeff-Kesselman · Jun 05, 2014 at 08:29 PM

NGUi is good for something , not for others.

The last time I did a health bar I did it very simply as two GUITextures. 'One was the outer frame and the other was a bar that filled the frame. I just scaled and repositioned the bar to get different levels.

Comment

People who like this

0 Show 0 · 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.

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

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

Related Questions

Healthbar above enemy : 2d 2 Answers

Destroying Enemy Help 5 Answers

Make HUD to show object direction 0 Answers

How can I do health bar follows enemy? 1 Answer

Health below zero but shows negative numbers 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