- Home /
GuiTexture above 3D objects
Hello,
I'm trying to put health bars above my bad guys and I want the bar to follow them and also always face the camera. I've watched and read a few tutorials but half of them don't work, maybe it's because I'm using the GUI.DrawTexture wrong.
So far I have this code which is attached to my gameobject GUITexture in the game (I basically found this code in the forum) :
public Texture myText;
public Transform target;
float x, y;
public float xOffset, yOffset;
// Update is called once per frame
void Update () {
Vector3 BarPosition;
BarPosition = Camera.main.WorldToScreenPoint (target.position);
x = BarPosition.x - xOffset;
y = Screen.height - BarPosition.y - yOffset;
Rect HPBAR = new Rect (x, y, 100, 20); // 100 and 20 Random values to test
GUI.DrawTexture (HPBAR, myText);
}
With this nothing shows up, whatever xOffset and yOffset might be. Target is supposed to by my bad guy and Texture is obviously my texture.
Any ideas ?
Answer by robertbu · Jan 25, 2014 at 07:24 PM
First off, GUI.DrawTexture() is not the same thing as a GUITexture. The reason your GUI.DrawTexture() is not showing up is that GUI commands must be executed in the OnGUI() callback. So you are going to have to do:
void OnGUI() {
Rect HPBAR = new Rect (x, y, 100, 20); // 100 and 20 Random values to test
GUI.DrawTexture (HPBAR, myText);
}
Thank you it was just that indeed, I feel stupid now. But thanks!
Your answer
Follow this Question
Related Questions
Help with destroying guiRect? 0 Answers
My GUI IS Bugging Out. Can Anyone Help? 0 Answers
How to make a texture cover an entire GUI box 0 Answers
Fill rectangle from bottom to top 1 Answer
How do I make a custom health bar? 0 Answers