I have a very simple healthbar OnGUI setup for my enemies:
void OnGUI()
{
// Get our position where the enemy is, tried setting this to values above/below Y axis, didn't work
Vector3 worldPosition = transform.position + new Vector3(0.0f, 0.0f, 0.0f);
Vector3 healthBarPos = Camera.main.WorldToScreenPoint(worldPosition);
// Draw a simple texture for a black background over the enemy
GUI.DrawTexture(new Rect(healthBarPos.x - 26, (Screen.height - healthBarPos.y) - 110, 62, 8), healthBackTexture);
// Draw a simple texture for a green health bar inside the black background, it scales off the health / maxHealth
GUI.DrawTexture(new Rect(healthBarPos.x - 25, (Screen.height - healthBarPos.y) - 109, 60 * (health / maxHealth), 6), healthTexture);
}
The healthbar displays over the enemy just fine, the problem I run in to is if I zoom in the healthbar will float down towards the feet of the model, this also happens for the GUI.Label I was using for the enemy name but I edited that out to make things super simple.
Here’s a quick youtube video show how it looks when i zoom in / out.
Is there a way to set the script to some how retain the position of always being over the enemies head and not float down when the camera zoom changes? I’m sure its some super simple thing for the position/viewport but I can’t seem to find anything or figure it out. I’m sure i’ll feel like an idiot once I figure it out. lol