Help me with Damage system please
Hey Guys, first , sorry about my English...
I'm having a logic problem in my C # script to calculate the damage received when it was attacked, Every time I click the ReceivedDamage button, it calls the function "public void RecebeDano ()", if i don't have the basicShield equipped, the received damage is correct, but, When basciShield is equipped (basicShieldActive = 1), when I press the receive damage button, the damage received does not subtract from the hp, but rather adds, i have two script, levelUp(Take care of the calculation of exp and level up) and GoldAndShop(take care of the shields, gold you have and prices), What could I be doing wrong?
float hp;
public Text hpText;
public GoldAndShop goldAndShop;
public void RecebeDano(){//The method that is going wrong
if(estado==1){
float damage = randomDamage();
float defenceDamage;
float finalDamage;
if(GoldAndShop.basicShieldActive == 0){//the damage receveid is correct
hp -= damage;
contDano++;
hpText.text = "Hp: " + hp;
Debug.Log("Damage Received: " + damage);
if(damage>60){Debug.Log("Critic");}
}
if(GoldAndShop.basicShieldActive == 1){//the damage received is wrong
defenceDamage = damage * GoldAndShop.basicDefence;
finalDamage = defenceDamage - damage;
hp -= finalDamage;
hpText.text = "Hp: " + hp;
contDano++;
Debug.Log("Damage Received: " + damage);
Debug.Log("Defended 10%");
Debug.Log("Defended: " + defenceDamage);
if(damage>60){Debug.Log("Critic");}
}
if(hp<=0){
estado = 0;
Debug.Log("GAME OVER");
}
}
}
Answer by springwater · Dec 27, 2016 at 06:16 PM
finalDamage = defenceDamage - damage;
hp -= finalDamage;
hpText.text = "Hp: " + hp; <----------CHANGE THIS TO MINUS MABEy?
contDano++;
Debug.Log("Damage Received: " + damage);
I suppose Your answer is probably incorrect. The line hpText.text = "Hp: " + hp;
is responsible for displaying string "Hp: 100" for exxample. That '+' operator is for adding strings together. I think my answer (below) to this thread is correct, if I'm not mistaken of course.
Answer by TheGreyRaveen · Dec 27, 2016 at 06:18 PM
From the picture I attached below you can see that there are two possibilites why is this happening: 1. damage < 0 AND GoldAndShop.basicDefence > 1 2. damage > 0 AND GoldAndShop.basicDefence < 1
I guess damage is rather higher than zero so the problem probably lies in basicDefence < 1. I suppose you wanted basicDefence to be some kind of a percentage multiplier. In that case you want to write the code like this:
if(GoldAndShop.basicShieldActive == 1){
hp -= damage * GoldAndShop.basicDefence;
Hope it helps!
Answer by ChellesGabriel · Dec 28, 2016 at 05:15 AM
Thanks for the personal help, I managed to fix it, it was only multiply by -1 that worked, now why I said I have no idea.
defenceDamage = damage * GoldAndShop.basicDefence;
finalDamage = defenceDamage - damage;
hp -= finalDamage * -1; // justa this multiplication, to transform the result to negative, Why??
hpText.text = "Hp: " + hp;