• 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
Question by hardmode2236 · Nov 23, 2014 at 05:23 AM · mathhealthbarformulahealth

health bar position formula

I need a formula to position my health bar object as it scales down. i got close but i just couldn't get it right.

i scale it down like so:

 transform.localScale.x = (maxSize*HP)/maxHP;

Then on my next line i need to reposition it based on how much it just shrank. otherwise my healthbar would shrink from both ends, instead of shrinking to the left.

the closest iv gotten was with t$$anonymous$$s formula:

 transform.localPosition.x = noHP_X+(HP*(-noHP_X/maxHP));

noHP_X is the desired X location when health is 0.

and maxHP_X is the desired X location for when health is full. t$$anonymous$$s variable isn't used though... might be apart of the problem. it seems like it should be in there i just dont know where.

its all good except for a small gap between the start of the health bar and its background, as the health goes down the gap slowly closes until zero HP, at w$$anonymous$$ch point its finaly in the right spot.

if anyone could help me correct my formula, or just show me a better way to do it that would be sweet.

Comment
rakkarage

People who like this

1 Show 0
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
Best Answer

Answer by rakkarage · Nov 23, 2014 at 06:46 AM

another way to do it is to use Image.Fill with a value from 0 to 1. and don't need to reposition http://docs.unity3d.com/460/Documentation/Manual/script-Image.html

Comment
hardmode2236

People who like this

1 Show 3 · 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 hardmode2236 · Nov 23, 2014 at 07:01 AM 0
Share

where do i get that script? i don't see it in the standard assets or components.

avatar image rakkarage · Nov 23, 2014 at 02:21 PM 0
Share

http://unity3d.com/unity/beta/4.6

avatar image hardmode2236 · Nov 23, 2014 at 07:56 PM 0
Share

oh wow the new UI things in that beta look amazing. thx for showing me this. :D

avatar image

Answer by LadyAth · Nov 23, 2014 at 03:19 PM

Or use the slider from the 4.6 UI. Take a look at http://unity3d.com/learn/tutorials/projects/survival-shooter/health-hud

Comment
rakkarage

People who like this

1 Show 0 · 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

Answer by uuil. · Nov 23, 2014 at 04:09 PM

You're problem is that you're not using a fullHP_X. As soon as you bring that into the equation it should work.

Get the initial x or length in units (during initialization or by hand will work). Then use:

 double foo = LENGTH_OF_HEALTH_BAR_IN_UNITS / 2;

The above calculates distance to the center of the health bar from the end.

 double foo = X_AT_FULL_HP - X_AT_NO_HP;

But t$$anonymous$$s one calculates the distance from the original position from the end position (since it will end up at the end of the bar).

 //Then you just scale it the same way you scale the length.
 transform.localPosition.x = X_AT_NO_HP + HP*foo;

The general formula be$$anonymous$$nd t$$anonymous$$s is scaling from a point w$$anonymous$$ch isn't the origin. And you do that by scaling all the elements (as you already are). And then scaling all the relative positions too. Then the solution is elegant and simple. Just scale position by HP.

T$$anonymous$$s formula should work for both GUI elements or GameObject elements. Put it all together:

 // I seperated the HP ratio. Mathematically it makes more sense
 // to scale by a ratio, rather than correct an arbitrary number.
 transform.localScale.x = maxSize * (HP/maxHP);

 // The above example as a one-liner.
 // hpBarX could be set in Start()
 // If it's not trivial to find hpBarLength, use the second example above instead.
 transform.position.x = hpBarX + hpBarLength/2 * (HP/maxHP);

Alternatively, you can just move the health bar mesh so that it's origin is at the end of the bar. Then when you scale it it scales about that point. Because then the relative position from the origin of the scale is (0,0[,0]) and you can ignore it. T$$anonymous$$s would also be ac$$anonymous$$eved by change the transformation anchor. IDK if 'anchor' or 'origin' is more applicable to Unity3D.

PS: I have found in the past that getting a pointer to the Transform component is a huge performance improvement. IDK if t$$anonymous$$s is still true, I've been away from Unity for some years.

transform_ = transform; in Start(), or better yet in the editor before runtime. Then use transform_.position

Comment
rakkarage

People who like this

1 Show 0 · 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

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

health drops to soon 1 Answer

Healthpickup isn't working 1 Answer

How to detect collisions in the children of a 2D object. 1 Answer

Destroy Player when Cur_Health <=0 1 Answer

Player health dropping far to fast 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