- Home /
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..
You could check out NGUI. It makes doing anything UI related much simpler.
@Ekta $$anonymous$$ehta 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.
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.
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.
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.
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.
Your answer
Follow this Question
Related Questions
Healthbar above enemy : 2d 2 Answers
Health below zero but shows negative numbers 1 Answer
Rect Following Object : Unity3d 1 Answer
Instantiate with healthbar dont work correctly 0 Answers
A node in a childnode? 1 Answer