- Home /
HealthBar for 5 Objects
Hello,
I am using the following script to display a healthbar for each object in my scene:
var soldier: Transform; var healthbarwidth : int = 80;
function OnGUI() { var screenPos : Vector3 = Camera.main.WorldToScreenPoint(soldier.position); GUI.backgroundColor = Color.green; GUI.Button(new Rect(screenPos.x - healthbarwidth/2, Screen.height - screenPos.y - 100, 80, 20),"Health"); }
My Problem now: As you can see I am referring to the Camera.main.WorldToScreenPoint of my object. It works fine but Unity displays each healthbar twice in the scene. In front of the camera and if I rotate the camera by 180 degrees there are the same healthbars again.
What's the problem of my script?
Thanks for your help :)
sry for that:
var soldier: Transform;
var healthbarwidth : int = 80;
function OnGUI() {
var screenPos : Vector3 = Camera.main.WorldToScreenPoint(soldier.position);
GUI.backgroundColor = Color.green;
GUI.Button(new Rect(screenPos.x - healthbarwidth/2, Screen.height - screenPos.y - 100, 80, 20),"Health");
}
Answer by Adamcbrz · Jul 21, 2013 at 10:20 PM
I don't think there is anything wrong with your script. The problem is that in both cases there wordltoscreenpoint return the same location. These ease solution is to check if the direction towards your object from the cameras forward is less then 90.
soldiersDirection = (soldier.transform.position - camera.transform.position).normalized;
float angle = Vector3.Angle(camera.transform.forward, soldiersDirection);
if(angle > 90)
{
// hide healthbar
}
That should help. Realize this is sent from my phone so there might be some typing errors.
Answer by moko · Jul 21, 2013 at 10:14 PM
I found a hint: http://answers.unity3d.com/questions/483118/gui-behind-camera-worldtoscreenpoint.html
...and tried to improve my code:
var soldier: Transform;
var healthbarwidth : int = 50;
var heading = soldier.position - Camera.main.transform.position;
function OnGUI() {
var screenPos : Vector3 = Camera.main.WorldToScreenPoint(soldier.position);
if (Vector3.Dot(camera.transform.forward, heading > 0){
GUI.backgroundColor = Color.green;
GUI.Button(new Rect(screenPos.x - healthbarwidth/2, Screen.height - screenPos.y - 100, 50, 20),"Health");
}
}
BUT it's not working :(
any idea?
Answer by IgorAherne · Jul 21, 2013 at 10:21 PM
var soldier: Transform;
var healthbarwidth : int = 80;
function OnGUI() {
var screenPos : Vector3 = Camera.main.WorldToScreenPoint(soldier.position);
var normScreenPos : Vector3 = soldier.position - transform.position;
normScreenPos = normScreenPos.normalized;
GUI.backgroundColor = Color.green;
if(Vector3.Dot(normScreenPos, transform.forward) > 0) //change value of greater to less than if needed
GUI.Button(new Rect(screenPos.x - healthbarwidth/2, Screen.height - screenPos.y - 100, 80, 20),"Health");
}
thanks guys. that helped a lot! :)
for anyone who's interested. thats my code now:
var soldier : GameObject[];
var healthbarwidth : int = 50;
var GUIEnabled = false;
function Start () {
soldier = GameObject.FindGameObjectsWithTag("soldier");
}
function OnGUI() {
for(var t : int = 0 ; t < soldier.length ; t++){
var screenPos : Vector3 = Camera.main.WorldToScreenPoint(soldier[t].transform.position);
var normScreenPos : Vector3 = soldier[t].transform.position - transform.position;
var soldiersDirection = (soldier[t].transform.position - camera.transform.position).normalized;
var angle : float = Vector3.Angle(camera.transform.forward, soldiersDirection);
if(GUIEnabled) {
GUI.backgroundColor = Color.green;
GUI.Button(new Rect(screenPos.x - healthbarwidth/2f, Screen.height - screenPos.y - 115f, 50f, 20f),"Health");
}
if(angle < 90){
GUIEnabled = !GUIEnabled;
}
}
}
Your answer
Follow this Question
Related Questions
How to set the position of a guitext using transform? 1 Answer
Trying to set up some GUI using planes and textures. Need help with dynamic changes. 0 Answers
positioning gui! 2 Answers
lock GUITexture on the screen at specific co - ordinates 3 Answers
GUI item with object position+dimensions 0 Answers