Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Jes · Sep 05, 2010 at 09:52 PM · cameraguipositionplayerhealth

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

7 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jes · Sep 06, 2010 at 07:48 PM 0
Share

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.

avatar image Jes · Sep 09, 2010 at 10:17 PM 0
Share

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

avatar image
1

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

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by adomanon · Dec 21, 2010 at 03:00 AM

could you post the new code ?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Kadu · Oct 31, 2011 at 11:42 PM

thanks, it helped me!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Can't get Gui Pos on top of Enemy ingame! 0 Answers

Restart with GUIText 2 Answers

There is no 'GUITexture' attached to the "Player Character Prefab(Clone)" game object, but a script is trying to access it. 2 Answers

player goes crazy when using this, help! 0 Answers

Camera rotation around player while following. 6 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges