- Home /
Can you make a GUI.Box follow a player's position?(SOLVED)
Hello, I am new to Unity so I've been using several tutorials to try different things
I used the Health Bar tutorial of http://www.burgzergarcade.com/hack-slash-rpg-unity3d-game-engine-tutorial and made some modifications to create mine.
And I want to know: is it possible to make a GUI.Box Rectangle follow a character's position so that it hovers above its head?
I tried using the ObjectLabel script http://www.unifycommunity.com/wiki/index.php?title=ObjectLabel
but this only works for GUI Text/Textures
I think it could be done if I modify the z position of the rectangle to follow the camera but I have tried several things and I can't make it work.
EDIT: I added the corrected script to help if someone had the same problem as me:
using UnityEngine; using System.Collections;
public class PlayerHealth : MonoBehaviour {
public int maxHealth = 100; public int curHealth = 100;
public float healthBarLength;
/public int healthBarWidth; public int healthBarHeight;/ public GUISkin healthBarSkin; public Vector3 screenPosition;
// Use this for initialization void Start () {
healthBarLength = Screen.width / 8;
}
// Update is called once per frame void Update () {
screenPosition = Camera.main.WorldToScreenPoint(transform.position);
screenPosition.y = Screen.height - screenPosition.y;
AddjustCurrentHealth(0);
}
private bool _mouseEnter = false;
void OnGUI() { if(!_mouseEnter) return; //draw your GUI stuff here with Unity's OnGUI code - see ref for details GUI.skin = healthBarSkin;
GUI.Label(new Rect(screenPosition.x - 36, screenPosition.y - 35, Screen.width / 8, 7), "Health");
GUI.Box(new Rect(screenPosition.x - 36, screenPosition.y - 35, healthBarLength, 7), "Health");
} void OnMouseEnter() { _mouseEnter = true; } void OnMouseExit() { _mouseEnter = false; }
public void AddjustCurrentHealth(int adj) {
curHealth += adj;
if(curHealth <= 0)
Destroy(gameObject);
if(curHealth > maxHealth)
curHealth = maxHealth;
if(maxHealth < 1)
maxHealth = 1;
healthBarLength = (Screen.width / 8) * (curHealth / (float)maxHealth);
}}
Answer by Jesse Anders · Sep 05, 2010 at 10:27 PM
One method you could try would be to use Camera.WorldToScreenPoint() to compute a screen-space point corresponding to the character's position. If you unproject a point at the top of the character's head and then offset the GUI element vertically from that point, you should end up with a GUI element that appears to float above the character.
Thank you for your answer, I'm still trying to get this to work but now I know what I'm supposed to be doing.
I tried again today and it worked! WorldToScreenPoint was what I needed and I updated the script to help others with the same problem.
Answer by adomanon · Dec 22, 2010 at 03:38 AM
Ty I solved this code is working for me :D
enter code here
using UnityEngine; using System.Collections;
public class EnemyHealth : MonoBehaviour { public int maxHealth=100; public int curHealth=50; public Vector3 screenPosition;
public float healthBarLenght;
// Use this for initialization void Start () { healthBarLenght=Screen.width/2; }
// Update is called once per frame void Update () { AddjustCurrentHealth(0);
} void OnGUI(){
screenPosition = Camera.main.WorldToScreenPoint(transform.position);
screenPosition.y = Screen.height - screenPosition.y;
GUI.Box(new Rect(screenPosition.x-10,screenPosition.y-40,healthBarLenght,20),curHealth+"/"+maxHealth);
}
public void AddjustCurrentHealth(int adj){ curHealth +=adj;
if(curHealth<1)
curHealth=0;
if(curHealth>maxHealth)
curHealth=maxHealth;
if(maxHealth<1)
maxHealth=1;
healthBarLenght=(Screen.width/2)*(curHealth/(float)maxHealth);
}
}
Answer by Velo222 · Oct 15, 2012 at 08:30 PM
This helped me tremendously, thank you. Every tutorial and youtube video I found was for a fixed healthbar, and not a dynamic one. This works great.
Your answer
Follow this Question
Related Questions
Can't get Gui Pos on top of Enemy ingame! 0 Answers
Restart with GUIText 2 Answers
player goes crazy when using this, help! 0 Answers