Anchor a health bar to a character/enemy
I have a game I'm working on where the player can see the health bars of the player's avatar and other creatures in the game. I want this health bar to always follow the character/creature around. However, I can't figure out how to do this. I thought having the position change when the respective object moved would do the trick, but apparently, it doesn't. When watching the health bar's transform, however, it shows that it is moving with it's respective object but I don't see it on the game screen. Can anyone explain how I go about accomplishing this? Here's the Dropbox link to the project.
Dropbox Link YouTube video of what it's currently doing
HealthBar Script
public class EnemyHealthBar:MonoBehaviour{
public Texture2D healthBar;
public Texture2D health;
public Rect healthBarPosition;
public Rect healthPosition;
public GameObject monster;
void Start(){
}
void Update(){
transform.position = new Vector3(monster.transform.position.x, monster.transform.position.y, monster.transform.position.z);
Debug.Log(transform.position);
}
void OnGUI(){
GUI.DrawTexture(healthBarPosition, healthBar);
}
}
Answer by Qvothe · Oct 19, 2016 at 02:31 AM
Make sure that your character is the parent to the healthbar, if the character is the parent object, then the healthbar will move (without any coding) so that its position remains relative to its parent.
You're going to need to add a "billboard" property to the healthbar. The "billboard" property makes sure that the healthbar is always perpendicular to the screen, so that no matter what your characters orientation in the game world, the text/image you're trying to display always shows correctly.
Also, you might need to adjust the position of the healthbar relative to the screen space. I haven't messed with this in a while, but iirc you have to adjust the Ypos by diving by 2 or something similar.
Honestly, it would probably be better/easier to make a generic prefab out of your healthbar, then instantiate it during runtime and make your character's avatar the parent object. This way you can edit the healthbar in play mode to get it exactly where you want it and how you want it to look instead of generating it via code at runtime only.
Answer by OutOfRam · Oct 18, 2016 at 10:54 PM
Have you tried simply parenting the healthbar to the monster instead of manually trying to move it? An object always stays in the same position relative to it's parent, unless otherwise told, so makeing the monster be the parent of the healthbar will keep it in the monsters local space
you can parent the object in the editor by dragging the healthbar object onto the monster object; or if it is instantiated mid game, you can do it through code using
healthBar.transform.parent = monster.transform;
I hope this helps!
I had it set as a child in the inspector thinking that would do it, but I always got the same results which made no sense to me as it should work, but it just stays in the top left hand corner of the screen and not over the parent object.
Well, is the healthbar part of a UI interface(an actual UI component), or is it a physical object, because the two should be handled differently?
Answer by OusedGames · Oct 19, 2016 at 12:38 AM
On my games I use a Canvas on top of the enemy:
and set the render mode of the canvas to "World Space"
inside the canvas I use an UI slider
And put this code on the canvas
public class BarFollow : MonoBehaviour {
public Transform enemyPos; Vector3 offset; void Start () { offset = transform.position - targetPos.position; } void Awake() { offset = transform.position - targetPos.position; } void LateUpdate () { transform.position = targetPos.position + offset; transform.LookAt(Camera.main.transform.position); } }
Hope it works!!