Hi, im writing a 2D Game and I have a power bar which shall be filled when an enemy is killed. The problem it isnt working. Oh and my Death Animation of the enemy is also not working, maybe you can help here too. Here’s my code:
public class Enemy : MonoBehaviour
{
public float health = 1;
public bool isDead = false;
public float damage = 1;
Animator anim;
MummyWalk walk;
HealthController healthController;
public float givePower = 1;
void ApplyDamage(float damage)
{
health -= damage;
if (health <= 0)
{
Debug.Log("ApplyDamage geht");
isDead = true;
Death();
}
}
void Death()
{
Debug.Log("Death Funktion geht");
isDead = true;
anim.SetTrigger("mummy_death");
walk.enabled = false;
healthController.AddPower(givePower);
}
public class HealthController : MonoBehaviour
{
public float power = 5;
public float startPower = 5;
void Start ()
{
healthcontroller = this;
anim = GetComponent<Animator>();
playerController = GetComponent<Walk>();
if(Application.loadedLevel == 0)
{
power = startPower;
}
else
{
power = PlayerPrefs.GetFloat("power");
}
UpdateView();
void UpdateView()
{
powerGUI.fillAmount = power / maxPower;
}
public void AddPower(float extraPower)
{
power += extraPower;
power = Mathf.Min(power, maxPower);
UpdateView();
}