- Home /
Health bar goes down instantly.
Hello. I am trying to create a health bar, and when my enemy attacks, it goes down. But when I run it, once my mutant attacks, my health goes down instantly. I'm guessing it's because of Update, but when I added a boolean it did the same things. If someone can help that would be much appreciated. Please keep in mind that I am new to Unity. Here is my code:`using System.Collections; using UnityEngine; using UnityEngine.UI;
public class CharacterHealth : MonoBehaviour {
public float CurrentHealth { get; set; }
public float MaxHealth { get; set; }
public Slider healthBar;
public GameObject mutant;
public bool hasMutantAttacked = false;
void Start()
{
MaxHealth = 20f;
// Resets health to full on game load
CurrentHealth = MaxHealth;
healthBar.value = CalculateHealth();
}
void Update()
{
if(mutant.GetComponent<EnemyController>().MutantAudio.isPlaying && !hasMutantAttacked)
{
hasMutantAttacked = true;
}
if(hasMutantAttacked && mutant.GetComponent<EnemyController>().MutantAudio.isPlaying)
{
DealDamage(6);
hasMutantAttacked = false;
}
}
void DealDamage(float damageValue)
{
// Deduct the damage dealt from the character's health
CurrentHealth -= damageValue;
healthBar.value = CalculateHealth();
// If the character is out of health, die!
if (CurrentHealth <= 0)
Die();
}
float CalculateHealth()
{
return CurrentHealth / MaxHealth;
}
void Die()
{
CurrentHealth = 0;
Debug.Log("Dead");
}
}`
Thanks!
The way you have it right now, as long as the audio is playing it will deal damage. Is this what you want?
If so keep in $$anonymous$$d that this operation happens very very fast, every frame. frame might only about 0.001 seconds depending on your computer. That's why it looks like it's going so fast, and it will look like as it is instant. try this: DealDamage(6 * Time.deltaTime);
this will slow it down.
also an unrelated thing. Try to cache components ins$$anonymous$$d of getting component every frame. It's not good for performance. Then use that "enemyControler.$$anonymous$$utantAudio.isPlaying" ins$$anonymous$$d
void Start()
{
EnemyController enemyController
=mutant.GetComponent<EnemyController>();
}
Thanks for the reply. I just want it to remove my health once when the enemy attacks once. I don't $$anonymous$$d how fast it goes down, as long as it goes down a certain amount. I really just want it to execute the line CurrentHealth -= damageValue;
one time when the mutant attacks.
Hello FlightCrazed. I was bored and was messing around in my script, and I actually came up with a script that got the effect I wanted. Sadly, I have no idea why it works. Can you explain to me why it works, and also maybe tell me any ways I could make my script better and get the same effect? Thanks. Also, I will make the EnemyController component later when I get the chance. Here is my new update method: void Update() { canDealDamage = !has$$anonymous$$utantAttacked; if(mutant.GetComponent<EnemyController>().$$anonymous$$utantAudio.isPlaying && !has$$anonymous$$utantAttacked) { has$$anonymous$$utantAttacked = true; } if(!mutant.GetComponent<EnemyController>().$$anonymous$$utantAudio.isPlaying) { has$$anonymous$$utantAttacked = false; } if(has$$anonymous$$utantAttacked == true && canDealDamage) { DealDamage(6); } }
Answer by Zaeran · Oct 12, 2018 at 03:41 PM
Each update loop, you're setting hasMutantAttacked to true as long as its audio is playing, and then checking if it's true to apply damage. This means that whenever the audio is playing, it will apply damage every frame.
Ideally, you'd make DealDamage a public function, then have the mutant call the function in its attack code.
Your answer
Follow this Question
Related Questions
Player health dropping far to fast 1 Answer
how do u make diffrent chacters and give them commands 2 do things 2 Answers
unity health bar not losing health 1 Answer
How to make my health decrease? 1 Answer
Health Code, Damage and Healing 0 Answers