- Home /
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
.
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);
}
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).
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
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);
}
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?
it will update the size as long health didn't pass the maxHealth.
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?
it will happen when health >= maxHealth then health = maxHealth so the statment will always be true and the bar wan't grow.
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
Your answer
Follow this Question
Related Questions
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