• 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
1
Question by sketchers1 · Feb 21, 2012 at 04:24 AM · texthealthnumberfloating

Healthy Floating Numbers

I have this code attached to some enemy characters in my scene. I was wondering if there was a way to have a health bar or health number just floating above the enemy characters?

 var hitPoints = 100.0;
 
 var deadReplacement : Transform;
 
 var dieSound : AudioClip;
 
 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)
     {
         Detonate();
     }
 }
 
 
 function Detonate () {
     // Destroy ourselves
     Destroy(gameObject);
     
     // Play a dying audio clip
     if (dieSound)
         AudioSource.PlayClipAtPoint(dieSound, transform.position);
 
     // Replace ourselves with the dead body
     if (deadReplacement) {
         var dead : Transform = Instantiate(deadReplacement, transform.position, transform.rotation);
         
         // Copy position & rotation from the old hierarchy into the dead replacement
         CopyTransformsRecurse(transform, dead);
     }
 }
 
 static function CopyTransformsRecurse (src : Transform,  dst : Transform) {
     dst.position = src.position;
     dst.rotation = src.rotation;
     
     for (var child : Transform in dst) {
         // Match the transform with the same name
         var curSrc = src.Find(child.name);
         if (curSrc)
             CopyTransformsRecurse(curSrc, child);
     }
 }
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

3 Replies

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

Answer by MithosAnnar · Feb 24, 2012 at 07:50 PM

Attach this script to your enemy.

EnemyHealth.js:

 /* Attach to the enemy */
 
  //the amount of health the enemy has
 var hP : float = 100;     
 
 //the amount of health the enemy should not have more of
 var maxHP : float = 100;      
 
 //the width your bar is
 var healthBarWidth : int;  
     
 //the texture you wish to have on 
 var EnemyHealthTexture : Texture;      
 
 //allows the bar to be seen when set to true
 var HealthEnabled : boolean;
 
 function Start () {
 
    //whatever length you want your bar to start at
    healthBarWidth = 131;
 
    //by default we can't see the health bar
    HealthEnabled = false;
       
 }
 
 function Update () {
 
    //the percent will update the length of the bar
    var healthpercent : float = hP / maxHP;
    
    if (healthpercent < 0) { healthpercent = 0; }
    if (healthpercent > 100) { healthpercent = 100; }
 
    //makes sure the bar is the correct ratio
    healthBarWidth = healthpercent * 131;
 
    
 }
 
 function OnGUI () {
 
 //if Activated show the health Bar
 if (HealthEnabled == true) {
 
    //Draw the health bar, set it to whatever position and size
    GUI.DrawTexture( Rect(60, 50, healthBarWidth, 17), EnemyHealthTexture);
    
 }
    
 }
 
 //when you target the enemy you also SendMessage("Activate");
 function Activate () {
 
   //allows the GUI to be Shown
    HealthEnabled = true;
 
 }
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
2

Answer by Berenger · Feb 21, 2012 at 06:18 AM

First I thought you were asking for variables of type float good for your health ^^

Anyway, Health bar is done can be displaying a white texture with a tint on Gui, rect.width being that health. Displaying health over a character's head, look Camera.ScreenPointToRay.

Comment
Add comment · Show 2 · 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 sketchers1 · Feb 21, 2012 at 01:59 PM 0
Share

haha!

I am pretty new at scripting.... How would I do that?

avatar image Berenger · Feb 21, 2012 at 03:32 PM 0
Share
  • Health bar with GUI :

Unity let you implement a function called OnGUI which will be called by Unity to display all the gui stuff. One way to display a texture is, inside OnGUI(), to call GUI.DrawTexture( rect, tex ); By modifying the width value of rect, you will then change the size of the health bar.

Another aspect of the GUI is GUI.color. That color will be multiplied to every GUI elements. It's set to white by unity before every OnGUI calls, so it doesn't change anything unless you want to. So if you display a white texture and set GUI.color to blue, your health bar is displayed blue.

  • Display something over the character's head

The challenge here is to find that head screen position. For that, you can use Camera.WorldToScreenPoint, which for a Vector3 in world space give you a Vector2 in screen space (0,0)->(resW, resH). Once you have that information you can display the text. Now a GUI is displayed from it's top left corner, so you'll need to center it. Last detail, don't call WorldToScreenPoint from OnGUI because the function is called twice by frame for GUI layout reasons, call it from update ins$$anonymous$$d.

avatar image
0

Answer by sketchers1 · Feb 25, 2012 at 05:41 PM

Thanks! I combined Your Script with mine to get something that works. I still have one problem though. I have multiple enemies. If I partially kill one then start killing another one, the health seems to jump up to 100% again. Is there a way to have separate health bars appear for different enemies? I have attached the script that I used:

var hitPoints : float = 100.0; var deadReplacement : Transform; var dieSound : AudioClip; var healthBarWidth : int; var EnemyHealthTexture : Texture; var HealthEnabled : boolean; var maxHP : float = 100;

function Start () {

//whatever length you want your bar to start at healthBarWidth = 131;

//by default we can't see the health bar HealthEnabled = false;

}

function Update () {

//the percent will update the length of the bar var healthpercent : float = hitPoints / maxHP;

if (healthpercent < 0) { healthpercent = 0; } if (healthpercent > 100) { healthpercent = 100; }

//makes sure the bar is the correct ratio healthBarWidth = healthpercent * 131; }

function OnGUI () {

//if Activated show the health Bar if (HealthEnabled == true) {

   //Draw the health bar, set it to whatever position and size
   GUI.DrawTexture( Rect(60, 50, healthBarWidth, 17), EnemyHealthTexture);

}

}

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 &lt;= 0.0)
 {
     Detonate();
 }

//allows the GUI to be Shown HealthEnabled = true;

}

function Detonate () { // Destroy ourselves Destroy(gameObject);

 // Play a dying audio clip
 if (dieSound)
     AudioSource.PlayClipAtPoint(dieSound, transform.position);

 // Replace ourselves with the dead body
 if (deadReplacement) {
     var dead : Transform = Instantiate(deadReplacement, transform.position, transform.rotation);

     // Copy position &amp; rotation from the old hierarchy into the dead replacement
     CopyTransformsRecurse(transform, dead);
 }

}

static function CopyTransformsRecurse (src : Transform, dst : Transform) { dst.position = src.position; dst.rotation = src.rotation;

 for (var child : Transform in dst) {
     // Match the transform with the same name
     var curSrc = src.Find(child.name);
     if (curSrc)
         CopyTransformsRecurse(curSrc, child);
 }

}

Comment
Add comment · Show 2 · 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 MithosAnnar · Feb 25, 2012 at 07:45 PM 0
Share

Ok, do this:

1 - attach the script to each enemy.

2 - after the lines:

if (healthpercent < 0) { healthpercent = 0; } if (healthpercent > 100) { healthpercent = 100; }

write: Debug.Log(healthpercent);

3 - tell me what pops up after that.

avatar image sketchers1 · Feb 28, 2012 at 03:01 AM 0
Share

an error occured....

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Using images to show numbers 1 Answer

Trying to figure out how to display floating damage numbers with armor taken into account 1 Answer

Health to come above enemy when clicked 1 Answer

Text looks weird? 1 Answer

How to display health on screen as text? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges