This fighter script works in conjunction with the script below (ClicktoMove.cs)
the idea is this script contains the individual elements for the combat functionality of the main character.
This includes
Click on Enemy
Player Health
Attack Animation
Death Animation
Player Damage
Player Attack Range and Impact Time
the idea is this script contains the individual elements for the combat functionality of the main character.
This includes
Click on Enemy
Player Health
Attack Animation
Death Animation
Player Damage
Player Attack Range and Impact Time
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;
// Use this for initialization
void Start () {
}
// 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)
{
opponent.GetComponent<mob>().getHit(damage);
impacted = true;
}
}
}
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 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;
// Use this for initialization
void Start () {
}
// 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)
{
opponent.GetComponent<mob>().getHit(damage);
impacted = true;
}
}
}
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;
}
}
}
}