For some reason with my script, when my character dies, he has god mode and cannot die again. Also, if my character is attacking and my health hits 0 whilst he’s attacking, he does not die and cannot die. I’ll attach the player script which includes his health and combat, and also if needed I can attach the enemy script too.
Any help is appreciated thanks!
using UnityEngine;
using System.Collections;
public class combat : MonoBehaviour
{
public GameObject opponent;
public AnimationClip attack;
public AnimationClip dieClip;
public float range;
public int damage;
public int health;
public int maxHealth;
private double impactLength;
public double impactTime;
public bool impacted;
bool started;
bool ended;
public float combatEscapeTime;
public float countDown;
// Use this for initialization
void Start ()
{
impactLength = (GetComponent<Animation>()[attack.name].length*impactTime);
}
// Update is called once per frame
void Update ()
{
Debug.Log (health);
if(Input.GetKey(KeyCode.Space)&&inRange ())
{
GetComponent<Animation>().Play(attack.name);
ClickToMove.attack = true;
if(opponent!=null)
{
transform.LookAt(opponent.transform.position);
}
}
if(GetComponent<Animation>()[attack.name].time<0.9*GetComponent<Animation>()[attack.name].length)
{
ClickToMove.attack = false;
impacted = false;
}
impact ();
die ();
}
void impact()
{
if(opponent!=null&&GetComponent<Animation>().IsPlaying (attack.name)&&!impacted)
{
if((GetComponent<Animation>()[attack.name].time)>impactLength&&(GetComponent<Animation>()[attack.name].time<0.9*GetComponent<Animation>()[attack.name].length))
{
countDown = combatEscapeTime + 2;
CancelInvoke ();
InvokeRepeating ("combatEscapeCountDown", 0, 1);
opponent.GetComponent<mob>().GetHit(damage);
impacted = true;
}
}
}
void combatEscapeCountDown()
{
countDown = countDown - 1;
if(countDown == 0)
{
CancelInvoke("combatEscapeCountDown");
}
}
public void GetHit(int damage)
{
health = health - damage;
if (health<0)
{
health = 0;
}
}
bool inRange()
{
if(Vector3.Distance (opponent.transform.position, transform.position) <= range)
{
return true;
}
else
{
return false;
}
}
//if dead returns true or alive turns false
public bool isDead()
{
if(health == 0)
{
return true;
}
else
{
return false;
}
}
void die()
{
if(isDead ()&&!ended)
{
if(!started)
{
ClickToMove.die = true;
GetComponent<Animation>().Play (dieClip.name);
started = true;
}
if(started&&!GetComponent<Animation>().IsPlaying(dieClip.name))
{
//what ever you want
maxHealth = 1000;
ended = true;
started = false;
ClickToMove.die = false;
}
}
}
}