- Home /
Minion floating healthbar over the head
Hey I am trying to make healthbars floating over the head of some soldiers, and I am trying to figure out what way to do this best.
Right now I am using a plane, whos size is dependent on the health left for the minion. It then looks at the camera.. however, since the camera is freefloating and can rotate around all axis, minions at the edge of the screen get a twisted healtbar, that does not look as I want it to.
I have tried with GUITexture, but could not really get that to work effeciently, as soon as I attach the GUITexture to my minions, it dissapears, and as far as I can understand this is bacuse the minions are not positioned (transform) with x,y between 0 and 1? I simply cant get it to do what I want it to. Also gui draw texture does not seem to be the correct way to go, or maybe I am not clever enough? :) I cant get this to stay at the correct position over the minions head.
So I am asking here, if anyone have some ideas, and experiences that they could share on the topic of creating floating healthbars?
Have it as a GUI element displayed at the corresponding position on screen space.
A starting idea would be:
screenPosition = Camera.main.WorldToScreenPoint(myObject.transform.position);
GUI.DrawTexture(new Rect(screenPosition.x, Screen.height - screenPosition.y - 96, 96, 64), someTexture);
Yes, I tried this, and it "kinda work" however, following the scale in 3d space is not really possible, so either the healtbars are way to big, or way to small. I dont know if there is any way to make sure that the scale in the gui space, follow the scale in 3d space.
Answer by Tabu · Dec 27, 2011 at 04:54 PM
So, I figured this out, and will here come with the sollution that did the trick for me. First of, the stuff that did not work for me was having doing gui.drawtexture. I almost got this working, but since I have a free floating camera, the size of the gui, would never fit to the size of the objects in the scene, it would almost fit when I rsized it basted on the distance to the camera, but as soon as I came in closer than 70 units from the 3d object, the size would no longer fit, and I would get all sorts of wierd results. (And since I am a Artist, doing complicated math is not really an option for me)
So I went back to my initial attempt, where I have a 3d object, that also have a LookAt towards the camera. As previaously stated, this made everything work fine, untill the camera came close to the units, as soon as the healthbars went a bit to the sides, the healthbar lookat direction would cause the healtbar to be sideways, and not looking straight at the camera. What I tried at last, and what I should have figured out in the beginning, was to just take the rotation of the camera and applie it to the healthbar... and there you have it.. a freefloating healthbar, looking straight at the camera.
using UnityEngine;
using System.Collections;
public class LookAtCamera : MonoBehaviour
{
private Camera theCamera;
// Use this for initialization
void Start()
{
theCamera = Camera.main;
}
// Update is called once per frame
void Update()
{
transform.rotation = theCamera.transform.rotation;
}
}