Enemy helth bar?

I can’t find any solution of this… I want to draw a gui texture above the gameobject in the 3d space using first person controller.

var target : Transform;
var texture : Texture;

function OnGUI() {
var viewPos : Vector3 = camera.WorldToViewportPoint (target.position);
GUI.DrawTexture(new Rect(viewPos.x, viewPos.y, 200, 50), texture);

}

With this code, I have my texture at the left top corner of the screen, but not at the target position, and it twitches when I moving the camera.

switch WorldToViewportPoint to WorldToScreenPoint

Here is how its looks like now :smile:
http://dl.dropbox.com/u/18714828/EnemyHealth/WebPlayer.html

It is made with the beta version of unity.

Problem is texture moving in Y axis up and down …

here is a working link

http://dl.dropbox.com/u/18714828/ENEMYHEALTH/WebPlayer/WebPlayer.html

var target : Transform;
var texture : Texture;

function OnGUI() {
    var viewPos : Vector3 = camera.WorldToScreenPoint (target.position);
    viewPos.y = Screen.height-viewPos.y;

    GUI.DrawTexture(new Rect(viewPos.x, viewPos.y, 200, 50), texture);
}

Thanks a lot,now it’s ok, but how can I make the texture don’t move, in depends of my distance from object?

http://dl.dropbox.com/u/18714828/ENEMYHEALTH/WebPlayer/WebPlayer/WebPlayer.html

Is it possible to make its looking like this allways?

And I find a bug, when u look at capsule, then turn view back 180 dgrees u will see this texture again just in the air… :smile:

For your first question, try creating an empty gameObject above your capsule at the point you want your health bar, and base the health bar’s position on that object (you’d have to change target to the new object).

kinda helps, but not alot…

Bug, when u look at capsule, then look back u will see this texture again just in the air…

link

Ok, it seems to be figured out:

var target : GameObject;
var texture : Texture;

var offsetY : float = 60;
var offsetX : float = 40;


function Update (){
}


function OnGUI() {
if (Vector3.Distance(transform.position, target.transform.position) < 15  target.renderer.isVisible){
	var viewPos : Vector2 = camera.main.WorldToScreenPoint (target.transform.position);
	GUI.DrawTexture(new Rect(viewPos.x - offsetX, Screen.height - (viewPos.y + offsetY), 75, 20), texture);
	}
}

But now I wondered how to find all targets (enemies) in the scene and draw their hp bars. I tried different ways but allways got an errors, can any1 help me here?

Ok, no1 help me, fine, I found the way my self)) :

var targets : GameObject[];
var texture : Texture;

var offsetY : float = 60;
var offsetX : float = 40;
var visibilityDistance : float = 25;
function Update (){
}


function OnGUI() {
	targets = GameObject.FindGameObjectsWithTag ("Enemy");
	for (var target : GameObject in targets){
		if (Vector3.Distance(transform.position, target.transform.position) < visibilityDistance  target.renderer.isVisible){
		var viewPos : Vector2 = camera.main.WorldToScreenPoint (target.transform.position);
		GUI.DrawTexture(new Rect(viewPos.x - offsetX, Screen.height - (viewPos.y + offsetY), 75, 20), texture);
		}
	}
}

How can I make the texture don’t scaling, in depends off my distance??