I have a propem i made a health gui texture and i want it be to attached to the enemy so as the enemy moves it moves with him…above him but it just moves with the camera
A GUI TEXTURE is a 2D flat image locked to the viewport screen.
By " attached to the enemy, I guess you mean it’s parented to the enemy ( a child of enemy ), this does not work.
What you need is to do is convert your enemy’s WORLD POSITION (Vector3 position or 3D position if you will) into —> VIEWPORT POSITION ( Vector2 position ).
To do this, you need to do something like this:
var enemy : Transform;
var healthBar : GUITexture;
var myCamera : Camera;
function Update(){
var enemyViewportPos : Vector3 = myCamera.WorldToViewportPoint (enemy.position);
// Look it up. enemyViewportPos is vector3 cause it stores if its behind or in front of you in the z axis.
if(enemyViewportPos.z < 0) // if behind
healthBar.position = Vector2(2,2); // Put out of sight or something
else
healthBar.position = Vector2(enemyViewportPos.x,enemyViewportPos.y);
}
I assume you are talking about a GUITexture, not a texture you are are drawing with the GUI class. GUITextures live in viewport space, but your enemy lives in world space. What you can is create an empty game object, make it a child of the enemy, and move it above the enemy. Then you will need a script to attach to your GUITexture that positions the texture at that point in viewport space. Something like:
var target : Transform;
function Update() {
transform.position = Camera.main.WorldToViewportPoint(target.position);
}