• 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 Yukuai · Sep 04, 2015 at 02:49 PM · rotationhealthbar

[3D][Unity5] Health Bar rotation problem.

Goodmorning,

I did a floating Health Bar (using canvas UI) that attach itself on an object and always rotate facing the main camera. All works well but I've got a problem: if the object start rotate on Z/X axys the canvas still be on the Y axis of the object.

I try to explain: if the object is a sphere and I attach a UI canvas on it I'll see the canvas above the sphere. If the sphere rotate on the Y axis (horizontal rotation) all is ok (che canvas is still above the sphere) but if the object start rotating on the X or Z axis (making a vertical rotation) the canvas that is attached to the Y axis rotate with the sphere.

How I can avoid this?

Comment
Add comment · Show 1
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 Yukuai · Sep 06, 2015 at 12:50 PM 0
Share

Sorry but I don't understand: - I create a new empty object; - Added the sphere to it; - Added the canvas to it;

What does you mean with "Apply translations to the parent"?

Sorry :D

3 Replies

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

Answer by Eudaimonium · Sep 06, 2015 at 09:03 PM

You are approaching the problem incorrectly.

If you want a health bar above an enemy (or something else in the game), you first take position of enemy, then add some Y value, so it's a bit above the enemy (for example).

Then, you take that position above the enemy, and you recalculate it from 3D world position, into 2D Screen position. You can use this function for such a calculation:
http://docs.unity3d.com/ScriptReference/Camera.WorldToScreenPoint.html

Then, you make a healthbar object and position it on the 2D screen position of your main HUD canvas.

That is how I would do it, at least.

There is also alternative way, if you just want to fix your current problem quickly:

Make a script that every enemy will have. Script must find the Healthbar object that is attached to the enemy. Then, it will simply position the healthbar object above the enemy, every frame. Can you do that, or do you need help with 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 Cryptomane · Sep 04, 2015 at 06:35 PM

Make them both children of the same object. Apply translations to the parent and rotations on the sphere only.

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 Yukuai · Sep 07, 2015 at 11:22 AM

So you suggest instead of having a Canvas attached on each object to have a picture (representing the healthbar) in the main HUD canvas and position it with the camera.WorldToScreenPoint?

Nice idea, I've to try it!

About the second solution: so instead of having a canvas attached to the object I position the canvas above it every frame? it could be good but the main HUD canvas seems to be a better solution :D

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.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to I make ui in world space look at the player? 2 Answers

Flip over an object (smooth transition) 3 Answers

Rotating Character 1 Answer

Make 3D Player look exactly at mouse with ScreenToWorldPoint? (maths Question) 2 Answers

Ensure free camera lines up behind player 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