I made a script for the enemy and when the health = 0. The problem is that it starts with 20 health but apparently the health drops to 0 at the start. There is a function that would remove 10 health from the health variable when hit, the enemy did not get hit and already the health depletes to 0.
Enemy Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour {
public static bool eh;
public float health = 20;
Animator anim;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
bool dead = false;
eh = Player.hit;
Debug.Log(health);
if (eh == false)
{
anim.Play("Enemy Stand");
}
if (eh == true)
{
StartCoroutine(hit());
health -= 10;
}
if (health == 0)
{
dead = true;
Debug.Log(dead);
}
}
IEnumerator hit()
{
yield return new WaitForSeconds(2);
anim.Play("Enemy_hit");
//yield return new WaitForSeconds(1);
eh = false;
}
}
player script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour {
private static bool movement;
private static bool attk_move;
private static bool moveback;
public static float player_health;
public static bool hit;
Animator anim;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
movement = Attack.move;
attk_move = Attack.attk;
moveback = Attack.moveback;
if(movement == true)
{
StartCoroutine(mf());
}
if (attk_move == true && moveback == false)
{
StartCoroutine(attack());
}
if (attk_move == false && moveback == true)
{
StartCoroutine(mb());
}
if(movement == false && attk_move == false)
{
anim.Play("Stand");
}
}
IEnumerator mf()
{
anim.Play("Walk FOrward");
transform.Translate(Vector2.left * 5f * Time.deltaTime);
yield return new WaitForSeconds(5);
}
IEnumerator attack()
{
anim.Play("Attack");
hit = true;
print("Hit");
yield return new WaitForSeconds(1);
hit = false;
}
IEnumerator mb()
{
transform.Translate(Vector2.right * 5f * Time.deltaTime);
yield return new WaitForSeconds(5);
//transform.Translate(Vector2.left * 0f * Time.deltaTime);
}
}