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 Nazathor · Mar 05, 2014 at 01:36 PM · c#2dbeginnerscalinghealthbar

Scaling related to damage

Hi everyone

(I am new to Unity, please keep that in mind, thank you)

Me and my friends are making a small 2D RPG-ish game. Currently I am trying to get our health bar to be reduced when the player character takes damage. To do this I have a red bar (health) and a black bar (depleted health), the black bar extends in one direction when the character takes damage, covering up the red bar. At the same time, a private integer named "health" counts down, and when it reaches zero, the game is over.

Here's my problem: If the player consumes a health potion, the health counter goes up and stops at a maximum. The health bar on the other hand, doesn't. This means that if you consume a potion so that your health would exceed the maximum health value, the black bar extends past the red bar.

Looks like this

alt text .

I have tried making an if-statement, but it didn't work. I am guessing this is because I am using transform.localScale to scale the black bar.

Is there something obvious I'm missing with transform.localScale or should I just use some other scaling method? If the later, which one?

I'll post my code as well, in case anyone would like to see it.

 void OnTriggerEnter2D(Collider2D other)
     {
         if(other.tag =="Enemy")
         {
             health = health - 1;
             damage.transform.localScale += new Vector3(0.1f,0,0);
         }
     
         if(other.tag =="HealthPotion")
         {
             health = health + 7;
             damage.transform.localScale -= new Vector3(0.7f,0,0);
             if(health >= maxHealth)
             {
                 health = maxHealth;
             }
             other.gameObject.SetActive(false);
         }


bar.png (5.8 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 Owen-Reynolds · Mar 05, 2014 at 02:38 PM 1
Share

Not sure about your code, but: localScale is the correct way to change scale, since it's the only way.

If you happen to have a parent, localScale works like you think -- it's your scale in proportion to the parent. So localScale=1 means "same scale as parent." Unlike position and localPosition, there's only localScale. So if your parent has scale 2 and you want to have scale 1, you have to do the math in your head, setting localSale to 0.5. That's a pain, and, I$$anonymous$$HO, if you want to change the scale of children, the parent should just stick with scale (1,1,1).

avatar image haim96 · Mar 05, 2014 at 02:53 PM 0
Share

not related but since you new, i would like to advice you to use other.comparetag("tag") ins$$anonymous$$d of other.tag=="tag" since it's faster and save memory. check this link: http://answers.unity3d.com/questions/40975/why-does-comparetag-exist.html

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by haim96 · Mar 05, 2014 at 03:00 PM

for your question, note that you scale your health outside of the if statement so it keep grow any way. you should do something like this:

 health = health + 7;
 if(health >= maxHealth)
      {
               health = maxHealth;
      }
     else
     {
     damage.transform.localScale -= new Vector3(0.7f,0,0);
     }
Comment
Add comment · Show 4 · 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 Nazathor · Mar 09, 2014 at 04:59 PM 0
Share

I am not 100% sure, but Unity wouldn't run the "else" statement if the first one is true, right? That would result in it only scaling the healthbar if the current health was 2 or 1 (in the case that maxhealth is 10), wouldn't it?

avatar image haim96 · Mar 09, 2014 at 05:30 PM 0
Share

it will update the size as long health didn't pass the maxHealth.

avatar image Nazathor · Mar 09, 2014 at 08:23 PM 0
Share

But won't health go past the maxhealth? Since health = maxHealth doesn't happen.

I had an idea of linking the scale of the black bar to the health by making health go from 0 -> 1. The problem occurred when I realized that I couldn't set the scale of the black bar, I could only increase or decrease it. Is there something I'm missing about localScale?

avatar image haim96 · Mar 09, 2014 at 08:46 PM 0
Share

it will happen when health >= maxHealth then health = maxHealth so the statment will always be true and the bar wan't grow.

avatar image
0

Answer by Kalmak · Mar 20, 2014 at 08:41 PM

Add a vector3 variable called damageStart that holds the size of your damage bar when the game starts. Reset the bar to that size if the health is equal to maxHealth. Here is an example using compareTag instead of tag==:

 public Vector3 damageStart;
     // Use this for initialization
     void Start () {
         damageStart = new Vector3(0.1f,0.5f,0.3f);
         damage.transform.localScale = damageStart;
     }
     
 
     void OnTriggerEnter(Collider other)
     {
         if(other.CompareTag("Enemy"))
         {
 
             health = health - 1;
 
             damage.transform.localScale += new Vector3(0.1f,0,0);
         }
         
         if(other.CompareTag("HealthPotion"))
         {
 
             health = health + 7;
 
 
             if(health >= maxHealth)
             {
                 health = maxHealth;
                 damage.transform.localScale = damageStart;
             }
             else
             {
                 damage.transform.localScale -= new Vector3(0.7f,0,0);
             }
             other.gameObject.SetActive(false);
         }
 }


HTH

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

23 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

Related Questions

I'm using an Object Pooler for the projectiles in my game. How do I reset their movement speed when they are made active again? 1 Answer

Button not working but other buttons are working. HELP 0 Answers

Complex object scaling 0 Answers

Multiple Cars not working 1 Answer

when changing size of gameobject bounds do not change 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