Please here i have a serious dude…
in vari enemy.health… enemy isnt a agameobject and i cant asign it
i want to asign here my opponent (target) and stop my player auto attack
when enemy health goes to 0. how can i do that???
void EnemyisDead()
{
if (enemy.health <= 0)
{
StopCoroutine(AutoAction());
}
}
and here this the completly script
using UnityEngine;
using System.Collections;
public class Player : Humans
{
public static Transform opponent;
public static bool isAttacking;
public static Player player;
Animation animation;
public AnimationClip attack;
public float attackImpact;
Enemy enemy;
void Awake ()
{
player = this;
health = maxHealth;
enemy = gameObject.GetComponent<Enemy>();
animation = GetComponent<Animation>();
initAnimations();
isAttacking = false;
}
IEnumerator AutoAction()
{
yield return new WaitForSeconds(1);
if(opponent != null && Vector3.Distance(opponent.position, transform.position) < range)
{
isAttacking = true;
animation.CrossFade(attack.name);
StartCoroutine(AutoAction());
}
}
void EnemyisDead()
{
if (enemy.health <= 0)
{
StopCoroutine(AutoAction());
}
}
//Initialize animations
void initAnimations()
{
animation = GetComponent<Animation>();
AnimationEvent attackEvent = new AnimationEvent();
attackEvent.time = attackImpact;
attackEvent.functionName = "impact";
attack.AddEvent(attackEvent);
}
void impact()
{
opponent.GetComponent<Creature>().GetHit(damage);
}
void Update ()
{
Attack();
EnemyisDead();
}
protected override void Attack ()
{
if(Input.GetKeyDown(KeyCode.Alpha1))
{
if(opponent != null && Vector3.Distance(opponent.position, transform.position) < range)
{
isAttacking = true;
animation.CrossFade(attack.name);
StartCoroutine(AutoAction());
}
}
if(!animation.IsPlaying(attack.name))
{
isAttacking = false;
}
}
}
and here is the another
using UnityEngine;
using System.Collections;
public abstract class Humans : MonoBehaviour
{
public string name;
public int damage;
public int maxHealth;
public int health;
public float range;
public float attackRate = 0.0f;
public void GetHit(int playerDamage)
{
health = health - playerDamage;
}
protected abstract void Attack();
}
thanks in advance