- Home /
Health Bar Centering
I currently have the framework in place to have a bunch of little enemies, all with their own health, and I was wondering how I would go about creating health bars that hover over their heads?
Ive got code that creates a GUI rect that scales with the players health, but the GUI stuff uses a different coordinate system(or so I think) that makes it difficult to center over the enemies.
Is there any easy way anyone can think of to create health bars that hover over the enemies on screen?
Thank you for any help :)
Answer by Motionreactor · Mar 15, 2010 at 01:50 PM
You can get the position of a 3D object in relation to the 2D screen with: Camera.WorldToScreenPoint
There is a related wiki entry which is fairly verbose, but is exactly what you want: Here
Answer by Jerad · Jun 14, 2010 at 03:01 PM
cam = GameObject.FindWithTag("Main Camera");// Try finding it with a tag instead
if (cam)// Make sure it exists, if it doens't this will get rid of the NullReferenceException all the same
{
ac = cam.camera.WorldToScreenPoint(gameObject.transform.position); //y-coordinate system needs to be inversed because 0-0 is top left
float y = ac.y;
y = Screen.height-y;
ac.y = y; //Define rectangle, x, y, width, height
Rect myRect = new Rect(ac.x-20,ac.y-30,50,3);
displaybar = myText1;
Debug.Log(myRect);
Debug.Log(displaybar.width);
Debug.Log(displaybar.name);
GUI.DrawTexture(myRect, displaybar, ScaleMode.ScaleAndCrop, false, 0);
}
Answer by Willem · Mar 15, 2010 at 08:09 PM
Thank you very much for your answer! I managed to figure it out :)
Now I am encountering another problem. Here is my code:
cam = GameObject.Find("Main Camera"); ac = cam.camera.WorldToScreenPoint(gameObject.transform.position);
//y-coordinate system needs to be inversed because 0-0 is top left
float y = ac.y;
y = Screen.height-y;
ac.y = y;
//Define rectangle, x, y, width, height
Rect myRect = new Rect(ac.x-20,ac.y-30,50,3);
displaybar = myText1;
Debug.Log(myRect);
Debug.Log(displaybar.width);
Debug.Log(displaybar.name);
GUI.DrawTexture(myRect, displaybar, ScaleMode.ScaleAndCrop, false, 0);
Sadly I get an error that says:
NullReferenceException: Object reference not set to an instance of an object
I am not sure what is generating this in the DrawTexture call, the myRect and displaybar seem to put out the right numbers in the debug log.
Any ideas?
It's not a forum like site @Willem if you want to post a question start a new one.. or add comments or edit the original post..
NullReferenceException (NRE) means there is a reference, which is like a link to another object, that is empty (null). In other words it can't find it, and thus the code errors as it can't continue. I would check your reference type variables, for example 'cam' and 'ac'.
Answer by vincent dupuis morneau · Jul 21, 2010 at 06:22 PM
well i think i know the error the script look fine but i think the reason why its not working is because your Main camera dont have the same name as the one in the script so thats why the script cant find the object.
if its not that im sorry but i dont know :S
Your answer
Follow this Question
Related Questions
Health Bar Only For Falling Damage 2 Answers
need help with GUI healthbar 1 Answer
Inverse health bar 2 Answers
Health bar by percentage? (not drawing a box) 4 Answers
Creating a Lifebar with simple GUI in C# 3 Answers