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 Haxxxxx · Mar 28, 2014 at 06:10 PM · guivector3class objecttower defensehealth bar

Enemy Health Bar problem Unity 2D.

Hi all,

I am building a tower defense game. I have an enemy prefab with a EnemyHealthBar empty game object attached to it, Health & Health-bg are both children objects and make up the health bar. I have the code to rescale my health bar upon taking damage however it doesn't seem to be working as expected.

My logic is working correctly, when the turret fires and hits a minion it deducts a life point out of a total of 6 and when it hits 0 the prefab enemy is destroyed. However my Health bar starts out like this:

alt text

After one hit it looks like this:

alt text

I can't upload another photo so to describe the last hit before death but its the same as picture 2 however the boxes are next to each other, separated by a few pixels. They're both a sixth of the original size.

Any help much appreciated!

Below is my health bar code:

 public class UIHealthBar: MonoBehaviour {
         
         public float initialGreenLength;
         public GameObject healthBar;
         public float curHealth;
         public float maxHealth;
         public float lastHealth;
         EnemyOgre healthScript;
         
         Vector3 greenPos;
         Vector3 greenScale;
         
         public float timer;
         
         void Awake ()
         {
             
             //loads enemy health value from healthScript
             healthScript = transform.parent.gameObject.GetComponent<EnemyOgre>();
             curHealth = healthScript.ogreHealth;
             maxHealth = healthScript.maxHealth;
             initialGreenLength=healthBar.transform.localScale.x;
             
             //stores two health values, will come in later
             lastHealth = curHealth;
         }
         
         
         void Update () 
         {
             
             timer -= Time.deltaTime;
             
             
             if(timer <= 0)
             {
                 timer = 0.05f;
                 
                 // adjusts the greenHealthBar so that as the enemy takes damage
                 // the bar shifts to the left, making it appear that the damage
                 // is coming off the right side of the bar and not equally from
                 // each end.
 
             if(lastHealth > curHealth)
             {
                 greenPos = healthBar.transform.localPosition;
                 greenPos.x = (lastHealth - curHealth)/curHealth;
                 healthBar.transform.localPosition = greenPos;
                 lastHealth = curHealth;
             }
             
             curHealth = healthScript.ogreHealth;
             maxHealth = healthScript.maxHealth;
 
             greenScale = healthBar.transform.localScale;
             greenScale.x = greenScale.x * (curHealth/maxHealth);
             transform.localScale = greenScale;
             
         }
     }
 }


untitled1.png (39.4 kB)
untitled.png (30.1 kB)
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 Haxxxxx · Mar 28, 2014 at 10:44 PM 0
Share

They don't! Ahhh thank you it was driving me crazy. I'll change that as soon as I'm home. Any idea why the scaling is changing for the background health bar as well as the main health bar? I've only attached the health bar gameObject to the UIHealthBar script.

avatar image Haxxxxx · Mar 28, 2014 at 11:34 PM 0
Share

I've done it! I deleted the healthbar parent object and made the two health bars both children of the Enemy prefab ins$$anonymous$$d. Thanks so much for making me realise such a simple error!

1 Reply

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

Answer by highpockets · Mar 28, 2014 at 08:40 PM

Do the white frame and the green health bar have the same position initially? I'm thinking that they have different origins in relation to the centre points, so initially it is ok because you never set the position of the green bar to the frame, but once you set it, it is off because of the difference of the points of origin.

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

21 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

Related Questions

Complex health bar using GUI.BeginGroup for clipping? 0 Answers

OnMouseUp() not working like meant 1 Answer

GUI planar aiming system help 0 Answers

GUI box scaling issue [C#] 0 Answers

Curved health bar 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