- Home /
Yet another health bar request
Hello everyone,
For the last month I searched and tried to implement a health bar in my game. I had no luck. Even though there are a million tutorials out there, I just didn't found the correct one. Trust me, I tried about 10-15 code variants.
I need this :
Floating health bar (a bar with color red for example, not a text showing a number) over each of my spawned units (follows the unit, is not set in an area of the screen). Would be nice to have the code in C#, if not I will try to adapt a JS code.
All I need is this magical piece of code which eludes me for so long. I even bought NGUIText and Health Bar scripts from the Asset Store. No luck as well..
Thanks everyone for the help. Got some nice tips and feedback here, even more concerning the fact that my question could've make me look like a lazy bum.
In case anyone stumbles like me I will provide the code that help me out as soon as I am back home !
Answer by Scribe · Jun 05, 2013 at 12:59 PM
This is not a place to ask for code to be written for you.
You should try and code something yourself but this is how I would go about it:
Either you need to make an Array/List of all your 'units' or you should instantiate each unit with the same script attached to it.
Once you have an object that needs a health bar, find its position (or a point above its position) in screen space using Camera.WorldToScreenPoint.
Once you have a point in screen space you can draw a rectangle using GUI.DrawTexture.
Scale the Texture based on the health of the unit
You should probably learn to code this if you wish to make a successful game,
A much better use of this site is to try and code it yourself and ask specific questions on sections of code
Scribe
EDIT: Attempt at code
int maxHealth;
int currentHealth;
Vector3 ScreenPos;
Texture2D MyTex;
int MaxTextureWidth;
void Update () {
ScreenPos = Camera.main.WorldToScreenPoint(transform.position+Vector3.up);
}
void OnGUI () {
GUI.DrawTexture(Rect(ScreenPos.x-((MaxTextureWidth/2.0)*((float)currentHealth/(float)maxHealth)), ScreenPos.y, MaxTextureWidth*((float)currentHealth/(float)maxHealth), 20), MyTex)
}
I agree with you that begging for code is not a very nice thing from my part, but I assure you I tried learning and search lots of references (I even used the google custom engine to search for the specific health bar articles in forum and in answers).
Frankly I know how to code all the elements you just pointed out, the problem is binding them together. I failed at this every time.
Is it possible to provide some pseudo code to see how to bind them, what functions to use etc ?
some probably very incorrect code but maybe it will help:
int maxHealth;
int currentHealth;
Vector3 ScreenPos;
Texture2D $$anonymous$$yTex;
int $$anonymous$$axTextureWidth;
void Update () {
ScreenPos = Camera.main.WorldToScreenPoint(transform.position+Vector3.up);
}
void OnGUI () {
GUI.DrawTexture(Rect(ScreenPos.x-(($$anonymous$$axTextureWidth/2.0)*((float)currentHealth/(float)maxHealth)), ScreenPos.y, $$anonymous$$axTextureWidth*((float)currentHealth/(float)maxHealth), 20), $$anonymous$$yTex)
}
I know UnityScript better than C# and that isn't at all tested so there are probably lots of errors, but have a go at implementing something similar
Scribe
EDIT
formatting seems to have failed so check my edited answer!
Answer by akstntn · Jun 05, 2013 at 01:36 PM
You have NGUI.. so I recommend you this :
http://www.youtube.com/watch?v=T9Q4mhGgDmg
http://www.youtube.com/watch?v=VONzxuzhGN8
http://www.youtube.com/watch?v=NC-jiZ5hdD8
good luck.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Screen.width ~ HealthBar problem - C# 2 Answers
Smooth tail followscript 0 Answers
Need help with enemy AI 1 Answer
health regeneration after death 1 Answer