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
0
Question by andrew_cs · Jan 16, 2014 at 07:05 PM · enemy healthalign two objects

Keep Health Bar Position as it Decreases In Size

Hello, I am working on my enemy health bars. They are sprites and children to the enemy game object. There is one red bar and one black bar. To start they are the same size and at the same position. When an enemy gets hit the red bar decreases in scale on the x. So far it decreases in size correctly when attacked. The problem is that when I change the size of the red bar it no longer is aligned with the black bar to the left. This leaves black areas on both the left and right side. I would like the two bars to keep the same x position so the lost health appears on the right side. This is what I thought would accomplish this.

 private GameObject healthBar;
 private Transform blackHealthBar;

 ....

 public void takeDamage(float damage){
     curHealth -= damage;
     
     if (curHealth <= 0) {
         Destroy(gameObject);        
     }
 
     float healthRatio = curHealth / originalHealth;
     healthBar.transform.localScale = new Vector3 (originalHealthBarX * healthRatio, healthBarYScale,
                                                   healthBarZScale);
     healthBar.transform.position = new Vector3 (blackHealthBar.position.x,
                                                 healthBar.transform.position.y,
                                                 healthBar.transform.position.z);
 }

I thought that changing the red health bar's x position to the black health bar's x position would solve this. Both of the x positions are the same even after changing the scale of the red bar. Does anyone know how I can resolve this issue or know a different approach?

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

1 Reply

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

Answer by filler03 · Jan 16, 2014 at 11:14 PM

You could use Renderer.Bounds to get the left-most point of the bar before it is scaled, then scale it. Then you could get the new bounds and find the difference, which is the distance you want to then move the bar to the left.

 float originalValue = renderer.bounds.min.x;
 
 //scale the bar
 
 float newValue = renderer.bounds.min.x;
 
 //calculate difference
 float difference = newValue - originalValue;
 
 //move the bar to the left
 transform.Translate(new Vector3(-difference, 0f, 0f);


Comment
Add comment · 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 andrew_cs · Jan 17, 2014 at 01:55 AM 0
Share

Brilliant! Thank you works perfect! I'd up-vote your answer but I don't have enough rep!

avatar image pjnovas · Jun 26, 2014 at 06:55 PM 0
Share

So clever! +1 I don't have enough rep to up vote ¬¬

avatar image nicmarxp · Mar 18, 2018 at 02:45 PM 0
Share

Great, thanks ! You forgot a ) before the ; on the last line :)

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

20 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

Related Questions

How do i Kill Enemy with Vehicle?? 2 Answers

How to rotate a player to match the terrain slope? 0 Answers

My wave system isn't working 0 Answers

Making enemy health drop if players sword hits enemy 1 Answer

[2D]Size of an object + align on grid 2 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