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 Jopan · Sep 20, 2014 at 06:49 PM · uihealthbarunity4.6

How can I use unity 4.6 gui to have a health bar that follows my enemy in world space?

From what I know of unity 4.6 gui so far, the only option I can think of is to create a world space canvas for each enemy just for the health bar. This will make for many many canvas in my scene. Is this the correct way to do this, or is there a better way? Again, I want to have each enemy have their own health bar hovering over their head in world space.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
12

Answer by Stardog · Jan 10, 2015 at 08:59 AM

This is the cleanest way I can find of making a UI element follow a world object's 3D position.

 public RectTransform canvasRectT;
 public RectTransform healthBar;
 public Transform objectToFollow;

 void Update()
 {
     Vector2 screenPoint = RectTransformUtility.WorldToScreenPoint(Camera.main, objectToFollow.position);

     healthBar.anchoredPosition = screenPoint - canvasRectT.sizeDelta / 2f;
 }

For many enemies, you will have to Instantiate the healthBar many times and store the instances in a list, then for loop through the list, setting their anchoredPosition.

Comment
Add comment · Show 7 · 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 Muhwu · Jan 17, 2015 at 07:45 AM 0
Share

Shouldn't World Space canvas be a working way to do this too, or have I completely misunderstood what that means. This particular solution (for me) requires additional smoothing to make it look better, it's a bit jittery as is.

avatar image Stardog · Jan 17, 2015 at 03:49 PM 0
Share

I noticed he wanted World Space, but I haven't seen a game that does that for health bars. Starcraft 2, Dark Souls, $$anonymous$$ass Effect, etc, all use Overlay. I guess it's jittery because it's using screen pixels, so I suppose you could smooth it and uncheck Pixel Perfect.

avatar image DRRosen3 · Feb 07, 2015 at 04:47 AM 0
Share

This code doesn't work. The health bar is appearing WAY off screen.

avatar image andreiamfg · May 11, 2015 at 04:18 AM 0
Share

It does work. Just be sure to check your Health Bar RectTransform Anchor and Pivot and adjust the Camera distance.

It doesn't work for every case, but after you make it work once you can adjust to your situation

avatar image Kerihobo · Jul 28, 2015 at 01:57 PM 0
Share

Cheers man! A+ solution!

Show more comments
avatar image
-2

Answer by GluedBrain · Sep 29, 2014 at 05:59 AM

That sounds quite normal, however, you should make sure that you save different kind of enemies as prefabs and use them as and when needed. In case you need an article on hoe to create a health bar check out the link below

TheGameContriver - Unity Health Bar

P.S. This would have been better posted in the forum rather though, as it was more of a discussion rather than a question.

Comment
Add comment · Show 1 · 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 jpthek9 · Apr 25, 2015 at 06:30 PM 0
Share

This does not answer the question at all. The health bar doesn't show over the enemy.

avatar image
0

Answer by Kiwasi · Sep 29, 2014 at 06:13 AM

Your onto the correct way to do it. Best bet is to include the canvas as a child object on whatever prefab you are using.

There are a couple options for the actual health bar too. You can use a slider. Or you can use any image and play with the fill effects.

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

29 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity4.6 - Setting text Property on Text Object 1 Answer

[uGUI] Performance on Color Lerping 0 Answers

How can I calculate whether an UI element is inside my camera view? 0 Answers

uGUI: Button that overlaps text 1 Answer

How do I layout dynamically instantiated UGUI components? 1 Answer


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