Hey I’m Adri.
I’m creating a tower defense and I created a health bar that cover all the enemies health, but iI got a problem with that. If you click like 3-4 times for second, It goes well, but if you click 6-7 times for second It starts to go up and down.
Thats my code for updating the bar and the text:
HealthBarFillAmount -= PlayerPrefs.GetFloat("DamageDone");
if (HealthBarFillAmount <= 0)
{
HealthBarFillAmount = 0;
}
waveHealthBar.fillAmount = HealthBarFillAmount / NextWaveHealth + 0.0f;
PlayerPrefs.SetFloat("DamageDone", 0);
EnemyHealthNow.text = HealthBarFillAmount.ToString("F2");
that’s the code that detects the damage done:
public void EnemyHit()
{
EnemyBase EnemyScript = enemyToShoot.GetComponent<EnemyBase>();
float EnemyHealth = EnemyScript.Health;
float HealthEnemyTotal = EnemyHealth + PlayerPrefs.GetFloat("MainDamage");
float DamageDone = HealthEnemyTotal - PlayerPrefs.GetFloat("MainDamage");
float DamageDoneIfItsUnderZero;
if (DamageDone >= 0)
{
PlayerPrefs.SetFloat("DamageDone", PlayerPrefs.GetFloat("MainDamage"));
}else
if (DamageDone < 0)
{
DamageDone *= -1;
DamageDoneIfItsUnderZero = PlayerPrefs.GetFloat("MainDamage") - DamageDone;
PlayerPrefs.SetFloat("DamageDone", DamageDoneIfItsUnderZero);
}
}
I call that void every time I click the screen.
I’m not good programming, I know, I just started on that because I’m studying 3D art and Design.
Sorry for my bad English.
Thanks.