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
0
Question by _TheInventor_ · Feb 03, 2018 at 09:03 PM · uiscalinghealthbar

Scale UI with distance

Hi! I am using Camera.main.WorldToScreenPoint for a health bar prefab I am using that displays on a canvas. How can I scale the size of the UI element of my health bar based on how far it is away to the Camera? I have a temporary system but it is completely trash. I want the health bars to look like they are floating above the enemies. Any help would be appreciated! Thanks.

Comment
Add comment · Show 2
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 Harinezumi · Feb 06, 2018 at 01:46 PM 0
Share

Why not use a world space canvas?

avatar image _TheInventor_ Harinezumi · Feb 06, 2018 at 02:51 PM 1
Share

I actually did switch to this once I found out I could do that. lol made my life easier

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by KittenSnipes · Feb 04, 2018 at 03:18 PM

@TheInventor

This will put the health on top of their heads. Put that script on your health texture or whatever it is and then drag the transform you wish it to follow.

 public Transform target;
  
 void Update (){
          var wantedPos = Camera.main.WorldToViewportPoint (target.position);
          transform.position = wantedPos;
 }


Comment
Add comment · Show 6 · 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 Glurth · Feb 04, 2018 at 03:54 PM 0
Share

I would also recommend you NOT put it on a canvas, which has the potential to do various transformation to its children objects, depending on the options you select for it.

avatar image _TheInventor_ · Feb 04, 2018 at 04:12 PM 0
Share

Thanks but I already have that working. I want to modify the transform.localScale based on how far the target position is away from the camera. $$anonymous$$oving closer would make the healthbar bigger, but moving away would make the healthbar shrink, as if floating in 3d space above the target object.

avatar image Glurth _TheInventor_ · Feb 04, 2018 at 05:51 PM 0
Share

This should happen automatically, just like it does to the character's themselves, if you DON'T put it on a canvas.

avatar image _TheInventor_ Glurth · Feb 04, 2018 at 06:57 PM 0
Share

OOOOH! I only skimmed over the code and thought Camera.main.WorldToViewportPoint was Camera.main.WorldToScreenPoint. Sorry about that!

Show more comments

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

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

How to adjust scale of UI panel as to fill percentage of the screen? 0 Answers

How to re-scale UI Mask in realtime 0 Answers

How do I create an undertale player health bar in C #? 1 Answer

Using Hinge Joint 2D with scaled UGUI 0 Answers

UI sprites doubles in size 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