x


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);
    }}
more ▼

asked Sep 05 '10 at 09:52 PM

Jes gravatar image

Jes
23 1 1 7

(comments are locked)
10|3000 characters needed characters left

5 answers: sort voted first

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.

more ▼

answered Sep 05 '10 at 10:27 PM

Jesse Anders gravatar image

Jesse Anders
7.3k 7 17 48

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.

Sep 06 '10 at 07:48 PM Jes

I tried again today and it worked! WorldToScreenPoint was what I needed and I updated the script to help others with the same problem.

Sep 09 '10 at 10:17 PM Jes
(comments are locked)
10|3000 characters needed characters left

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);
    }

}

more ▼

answered Dec 22 '10 at 03:38 AM

adomanon gravatar image

adomanon
17

(comments are locked)
10|3000 characters needed characters left

could you post the new code ?

more ▼

answered Dec 21 '10 at 03:00 AM

adomanon gravatar image

adomanon
17

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Oct 15 '12 at 08:30 PM

Velo222 gravatar image

Velo222
15 1 1 2

(comments are locked)
10|3000 characters needed characters left

thanks, it helped me!

more ▼

answered Oct 31 '11 at 11:42 PM

Kadu gravatar image

Kadu
1 1 1 2

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3698
x3015
x888
x810
x384

asked: Sep 05 '10 at 09:52 PM

Seen: 4100 times

Last Updated: Oct 15 '12 at 08:30 PM