so changes have been made to the Mob CS & Fighter CS to allow implementation of the Health Script and functionality of getting out of combat to allow the Health GUI to disappear from the screen when not in combat
FIGHTER.CS
using UnityEngine;
using System.Collections;
public class Fighter : MonoBehaviour {
public GameObject opponent;
public AnimationClip attack;
public AnimationClip DieClip;
public int damage;
public int health;
public double impactTime;
public double impactLength;
public bool impacted;
public float range;
bool started;
bool ended;
public float combatEscapeTime;
public float countDown;
// Use this for initialization
void Start () {
impactLength = (animation[attack.name].length*impactTime);
}
// Update is called once per frame
void Update () {
//Debug.Log(opponent);
if(Input.GetKey(KeyCode.Space)&&inRange())
{
animation.Play(attack.name);
ClicktoMove.attack = true;
if(opponent!=null)
{
transform.LookAt(opponent.transform.position);
//opponent.GetComponent<mob>().getHit(damage);
}
}
if(animation[attack.name].time>0.9*animation[attack.name].length)
{
ClicktoMove.attack = false;
impacted = false;
}
impact();
die ();
}
void impact()
{
if(opponent!=null&&animation.IsPlaying(attack.name)&&!impacted)
{
if((animation[attack.name].time)>impactLength&&animation[attack.name].time<0.9*animation[attack.name].length)
{
countDown = combatEscapeTime + 2;
CancelInvoke("combatEscapeCountDown");
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 return trues or else returns false
public bool isDead()
{
if (health ==0)
{
return true;
}
else
{
return false;
}
}
void die()
{
if(isDead ()&&!ended)
{
if(!started)
{
ClicktoMove.die = true;
animation.Play(DieClip.name);
started = true;
}
if (started&&animation.IsPlaying(DieClip.name))
{
//what ever you want to do
//set respawn point etc
ended = true;
started = false;
ClicktoMove.die=false;
}
}
}
}
using UnityEngine;
using System.Collections;
public class Fighter : MonoBehaviour {
public GameObject opponent;
public AnimationClip attack;
public AnimationClip DieClip;
public int damage;
public int health;
public double impactTime;
public double impactLength;
public bool impacted;
public float range;
bool started;
bool ended;
public float combatEscapeTime;
public float countDown;
// Use this for initialization
void Start () {
impactLength = (animation[attack.name].length*impactTime);
}
// Update is called once per frame
void Update () {
//Debug.Log(opponent);
if(Input.GetKey(KeyCode.Space)&&inRange())
{
animation.Play(attack.name);
ClicktoMove.attack = true;
if(opponent!=null)
{
transform.LookAt(opponent.transform.position);
//opponent.GetComponent<mob>().getHit(damage);
}
}
if(animation[attack.name].time>0.9*animation[attack.name].length)
{
ClicktoMove.attack = false;
impacted = false;
}
impact();
die ();
}
void impact()
{
if(opponent!=null&&animation.IsPlaying(attack.name)&&!impacted)
{
if((animation[attack.name].time)>impactLength&&animation[attack.name].time<0.9*animation[attack.name].length)
{
countDown = combatEscapeTime + 2;
CancelInvoke("combatEscapeCountDown");
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 return trues or else returns false
public bool isDead()
{
if (health ==0)
{
return true;
}
else
{
return false;
}
}
void die()
{
if(isDead ()&&!ended)
{
if(!started)
{
ClicktoMove.die = true;
animation.Play(DieClip.name);
started = true;
}
if (started&&animation.IsPlaying(DieClip.name))
{
//what ever you want to do
//set respawn point etc
ended = true;
started = false;
ClicktoMove.die=false;
}
}
}
}
MOB.CS
using UnityEngine;
using System.Collections;
public class mob : MonoBehaviour {
public float speed;
public float range;
public Transform player;
public CharacterController controller;
private Fighter opponent;
public AnimationClip enemyattack;
public AnimationClip death;
public AnimationClip run;
public AnimationClip idle;
public double impactTime = 0.36;
private bool impacted;
public int maxHealth;
public int health;
public int damage;
// Use this for initialization
void Start () {
health = maxHealth;
opponent = player.GetComponent<Fighter> ();
}
// Update is called once per frame
void Update () {
Debug.Log(health);
// below makes enemy chase _ is not required atm
/*if(inRange())
{
chase();
}
else
{
animation.Play(death.name);
} */
//Debug.Log(inRange ());
if (!isDead ())
{
if(inRange ())
{
chase ();
}
else
{
//animation.CrossFade(idle.name);
animation.Play(enemyattack.name);
attackMethod();
if(animation[enemyattack.name].time >0.9*animation[enemyattack.name].length)
{
impacted = false;
}
}
}
else
{
dieMethod();
}
}
void attackMethod ()
{
if(animation[enemyattack.name].time>animation[enemyattack.name].length*impactTime&&!impacted&&animation[enemyattack.name].time<0.9*animation[enemyattack.name].length)
{
opponent.getHit(damage);
impacted = true;
}
}
bool inRange ()
{
if(Vector3.Distance(transform.position, player.position)<range)
{
return true;
}
else{
return false;
}
}
public void getHit(int damage){
health = health - damage;
if(health<0){
health = 0;
}
}
void chase ()
{
transform.LookAt(player.position);
controller.SimpleMove(transform.forward*speed);
animation.CrossFade(run.name);
}
void dieMethod()
{
animation.Play (death.name);
if(animation[death.name].time>animation[death.name].length*0.9)
{
Destroy(gameObject);
}
}
bool isDead()
{
if(health<= 0)
{
return true;
}
else
{
return false;
}
}
void OnMouseOver()
{
//Debug.Log("Mouse Is Over This Mob");
player.GetComponent<Fighter>().opponent = gameObject;
}
}
using UnityEngine;
using System.Collections;
public class mob : MonoBehaviour {
public float speed;
public float range;
public Transform player;
public CharacterController controller;
private Fighter opponent;
public AnimationClip enemyattack;
public AnimationClip death;
public AnimationClip run;
public AnimationClip idle;
public double impactTime = 0.36;
private bool impacted;
public int maxHealth;
public int health;
public int damage;
// Use this for initialization
void Start () {
health = maxHealth;
opponent = player.GetComponent<Fighter> ();
}
// Update is called once per frame
void Update () {
Debug.Log(health);
// below makes enemy chase _ is not required atm
/*if(inRange())
{
chase();
}
else
{
animation.Play(death.name);
} */
//Debug.Log(inRange ());
if (!isDead ())
{
if(inRange ())
{
chase ();
}
else
{
//animation.CrossFade(idle.name);
animation.Play(enemyattack.name);
attackMethod();
if(animation[enemyattack.name].time >0.9*animation[enemyattack.name].length)
{
impacted = false;
}
}
}
else
{
dieMethod();
}
}
void attackMethod ()
{
if(animation[enemyattack.name].time>animation[enemyattack.name].length*impactTime&&!impacted&&animation[enemyattack.name].time<0.9*animation[enemyattack.name].length)
{
opponent.getHit(damage);
impacted = true;
}
}
bool inRange ()
{
if(Vector3.Distance(transform.position, player.position)<range)
{
return true;
}
else{
return false;
}
}
public void getHit(int damage){
health = health - damage;
if(health<0){
health = 0;
}
}
void chase ()
{
transform.LookAt(player.position);
controller.SimpleMove(transform.forward*speed);
animation.CrossFade(run.name);
}
void dieMethod()
{
animation.Play (death.name);
if(animation[death.name].time>animation[death.name].length*0.9)
{
Destroy(gameObject);
}
}
bool isDead()
{
if(health<= 0)
{
return true;
}
else
{
return false;
}
}
void OnMouseOver()
{
//Debug.Log("Mouse Is Over This Mob");
player.GetComponent<Fighter>().opponent = gameObject;
}
}