• 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
0
Question by ryba · Jan 02, 2013 at 09:13 AM · guiperformancehud

Best way to create mini hp-bars for enemies.

What i need is a little bar right above every single enemy unit, that indicates how much hp does t$$anonymous$$s unit have.

I see two possibilities:

  1. Create 2 box game objects that have rotation equal to camera (so it will always look as simple bar, that doesn't rotate) - one bar for hp part (red), and second for rest part of bar (black). At every hp change bars lengths changes.

  2. Make two GUI labels (two in the same sense as with boxes).

About boxes, i see 2 ways of changing their position to stay above unit w$$anonymous$$le it moves: 1. One big controller that manages all bars, setting at each frame their position equals to enemy unit game object + some offset. 2. Bar simply attached to game object as c$$anonymous$$ld.

About gui label i see only one way of managing its position: at each frame calculate game object position vector from world to screen (Camera.main.WorldToScreenPoint)

So there are kind of 3 ways to manage hp bars that i thought of (2 ways of box solution and 1 way with GUI labels).

What do you t$$anonymous$$nk is best in manner of performance ? Also how do you t$$anonymous$$nk, how heavy solutiona are comparing to each other ?

If you have any other solution it would be also cool to know ;]

There will be many of those bars, because im working on hack'n'slash game, like diablo or torchlight, with armies of enemies on the screen ;] Thats why i want to look at the problem from performance side.

Unfortunatelly i dont know any other proper way to do HUDs then GUI, but since i read about GUI that is often bottleneck, i would like to do my work on hp bars properly right now, not when i find out that slows game down terribly.

Comment
Add comment · Show 4
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 StephanK · Jan 02, 2013 at 10:14 AM 0
Share

Is it 2D or 3D?

avatar image ryba · Jan 02, 2013 at 10:24 AM 0
Share

Game is 3d, camera can rotate around player. Perspective is like in diablo, torchlight, but problematic can be that player is able to zoom in / out cammera, and rotate that. So boxes should have rotation identical to camera, so it will always look like simple rectangle.

avatar image StephanK · Jan 02, 2013 at 10:28 AM 0
Share

Do you want them to scale according to distance, or should they always be the same size regardless of how big/small the actual unit is?

avatar image ryba · Jan 02, 2013 at 10:32 AM 0
Share

Hard to say at this moment, best would be have both options to test them to determine which solution looks better.

1 Reply

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

Answer by StephanK · Jan 02, 2013 at 10:47 AM

If you want them to scale I'd suggest to go the hp-bar as c$$anonymous$$ld way and have one function that adjusts the rotation of all of them (preferably only those visible). If scaling is not wanted I'd probably attach an empty GO to each unit as an anchor point, then calculate the screen position of each anchor point (Camera.WorldToScreenPoint) an draw the actual hp bar in your GUI system using those screen coordinates. The first approach will automatically scale your bars, the second won't.

If you want to avoid the Unity gui system you can setup a second camera just for your hp bars and use orthograp$$anonymous$$c mode. If you set orthograp$$anonymous$$c size = Screen.height / 2 you can position your objects using screen coordinates.

Comment
Add comment · Show 4 · 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 ryba · Jan 02, 2013 at 10:54 AM 0
Share

Do you know how looks performance for those solutions ? Maybe it could be possible to calculate scale factor for boxes to keep them at one size ?

From coding perspective both gui and boxes seems to be light, so most important is performance :) How about mixing additional camera just for hp bars - does it perform better comparing GUI labels + camera.WorldToScreenPoints for each label in each OnGUI ?

avatar image StephanK · Jan 02, 2013 at 11:07 AM 0
Share

Doing any kind of calculation in OnGUI is a bad idea. The extra camera approach will probably be faster if you compare it to using GUI.Label, you will still have to use camer.ScreenToWorldPoint. Sure you could calculate a scale factor but then i'd go the second way so you don't have to. Performance wise I'm not sure, I'd guess that the first approach is a bit faster as you only have to "calculate" the correct orientation once (you can use Quaternion.LookRotation) and then just update the rotation. For the second rotation you will have to do ScreenToWorldPoint once per bar and then either update the position of the object (using separate camera) or draw a label at that position.

avatar image ryba · Jan 02, 2013 at 11:17 AM 0
Share

Problem with second position is that i have to calculate Screen to world point every frame, or at least very frequently, because when i move camera, my coordinates of label will mess up (object on scenes will change their positions, but labels wont, so i will need to calculate position from begining, everytime).

I think i stick with game object bars, but first ill need to write calculation of object scale factor, to keep size looking static.

Thx for this help, ill upvote your answer, but not yet, because i cannot (You don't have permission to do this action.) - this answer hub is configured really idiotic :/ When i get rights i'll reward you with reputation ;]

avatar image StephanK · Jan 02, 2013 at 11:29 AM 0
Share

Another approach would be to use a second camera that is orthographic, renders only your bars and is attached to the main camera. That way you won't have to update the size of your bars.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

CoolDown Animation on the GUI 1 Answer

GUI font size? 1 Answer

"NullReferenceException" while trying to draw a texture (C#) 1 Answer

Off Screen direction indicator HUD element / wrestling with Quaternion 1 Answer

World canvas with screen space canvas performance mobile ? 0 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