- Home /
C# Floating Health Bar
I had this working but I messed up and now im very frustrated.. so any help please.. I need a script working in C# to have a GUITexture float above a gameobject! Please and thank you!!
Answer by AeonIxion · Jan 14, 2014 at 12:45 PM
Take a look at WorldToScreenPoint
Vector3 screenPos = camera.WorldToScreenPoint(player.position);
Then you can use screenPos to set your texture on the right position
Is there anyway to make it so for each enemy the script is attached to it spawns a texture for them? I have a lot of enemies.
Also.. How would I set this up to have a texture follow my object..
Answer by woodoo · Jan 14, 2014 at 01:34 PM
You can set your texture transform parent as the GameObject you want it to follow. For example:
public GUITexture healthBar;
public GameObject healthyObject;
void Awake()
{
healthBar.transform.parent = healthyObject.transform;
}
It is just an coded example for this situation, you can also put the texture as child of the GameObject by the Unity Editor.
Setting it up, the health bar position will be related to the object position.
Your answer
![](https://koobas.hobune.stream/wayback/20220613130339im_/https://answers.unity.com/themes/thub/images/avi.jpg)