- Home /
2D GUI Group moving with players.
I have four players that have health and energy. The bars should all float just above the player's heads, and move/update along with them.
I cannot figure out how to make the gui I have move, specifically because I'm using a "group", and I'm not sure I fully understand it, or what to use instead.
Here is the current code I use, which only places it in the corner:
var barDisplay : float = 0;
var pos : Vector2 = new Vector2(20,40);
var size : Vector2 = new Vector2(60,20);
var healthBarEmpty : Texture2D;
var healthBarFull : Texture2D;
function OnGUI()
{
// draw the background:
GUI.BeginGroup (new Rect (pos.x, pos.y, size.x, size.y));
GUI.Box (Rect (0,0, size.x, size.y),healthBarEmpty);
// draw the filled-in part:
GUI.BeginGroup (new Rect (0, 0, size.x * barDisplay, size.y));
GUI.Box (Rect (0,0, size.x, size.y),healthBarFull);
GUI.EndGroup ();
GUI.EndGroup ();
}
function Update()
{
// for this example, the bar display is linked to the current time,
// however you would set this value based on your desired display
// eg, the loading progress, the player's health, or whatever.
barDisplay = health;
}
Health is a separate variable that starts at 100 and decreases as they take damage.
I considered just using several drawn boxes, and changing the size of that. Would that be a suitable way to do this? Is there an easy way to move this group?
Answer by Cherno · Aug 21, 2014 at 11:07 AM
Use Camera.WorldToScreenPoint to get the screen coordinates of your characters, and use these values to draw whatever you want to draw in your OnGUI function.