Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Willem · Mar 15, 2010 at 01:07 PM · guicenterprogress-barhealth

Health Bar Centering

I currently have the framework in place to have a bunch of little enemies, all with their own health, and I was wondering how I would go about creating health bars that hover over their heads?

Ive got code that creates a GUI rect that scales with the players health, but the GUI stuff uses a different coordinate system(or so I think) that makes it difficult to center over the enemies.

Is there any easy way anyone can think of to create health bars that hover over the enemies on screen?

Thank you for any help :)

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

4 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Motionreactor · Mar 15, 2010 at 01:50 PM

You can get the position of a 3D object in relation to the 2D screen with: Camera.WorldToScreenPoint

There is a related wiki entry which is fairly verbose, but is exactly what you want: Here

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
2

Answer by Jerad · Jun 14, 2010 at 03:01 PM

cam = GameObject.FindWithTag("Main Camera");// Try finding it with a tag instead
if (cam)// Make sure it exists, if it doens't this will get rid of the NullReferenceException all the same
{
    ac  = cam.camera.WorldToScreenPoint(gameObject.transform.position);    //y-coordinate system needs to be inversed because 0-0 is top left
    float y = ac.y;
    y = Screen.height-y;
    ac.y = y;    //Define rectangle, x, y, width, height
    Rect myRect = new Rect(ac.x-20,ac.y-30,50,3);
    displaybar = myText1;
    Debug.Log(myRect);
    Debug.Log(displaybar.width);
    Debug.Log(displaybar.name);
    GUI.DrawTexture(myRect, displaybar, ScaleMode.ScaleAndCrop, false, 0); 
}

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
-5

Answer by Willem · Mar 15, 2010 at 08:09 PM

Thank you very much for your answer! I managed to figure it out :)

Now I am encountering another problem. Here is my code:

cam = GameObject.Find("Main Camera"); ac = cam.camera.WorldToScreenPoint(gameObject.transform.position);

 //y-coordinate system needs to be inversed because 0-0 is top left
 float y = ac.y;
 y = Screen.height-y;
 ac.y = y;

 //Define rectangle, x, y, width, height
 Rect myRect = new Rect(ac.x-20,ac.y-30,50,3);
 displaybar = myText1;
 Debug.Log(myRect);
 Debug.Log(displaybar.width);
 Debug.Log(displaybar.name);
 GUI.DrawTexture(myRect, displaybar, ScaleMode.ScaleAndCrop, false, 0); 

Sadly I get an error that says:

NullReferenceException: Object reference not set to an instance of an object

I am not sure what is generating this in the DrawTexture call, the myRect and displaybar seem to put out the right numbers in the debug log.

Any ideas?

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 Lipis · Mar 15, 2010 at 08:14 PM 1
Share

It's not a forum like site @Willem if you want to post a question start a new one.. or add comments or edit the original post..

avatar image Digitalos · Jun 14, 2010 at 03:06 PM 0
Share

NullReferenceException (NRE) means there is a reference, which is like a link to another object, that is empty (null). In other words it can't find it, and thus the code errors as it can't continue. I would check your reference type variables, for example 'cam' and 'ac'.

avatar image
0

Answer by vincent dupuis morneau · Jul 21, 2010 at 06:22 PM

well i think i know the error the script look fine but i think the reason why its not working is because your Main camera dont have the same name as the one in the script so thats why the script cant find the object.

if its not that im sorry but i dont know :S

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

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

No one has followed this question yet.

Related Questions

Health Bar Only For Falling Damage 2 Answers

need help with GUI healthbar 1 Answer

Inverse health bar 2 Answers

Health bar by percentage? (not drawing a box) 4 Answers

Creating a Lifebar with simple GUI in C# 3 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