I tried several ways, but it seems no matter what I try it doesn’t work. He either sits still before he dies or the game object goes crazy and flys off screen.
using UnityEngine;
using System.Collections;
using SpriteFactory;
public class EnemyAi : MonoBehaviour {
public Transform target;
public int moveSpeed;
public int rotationSpeed;
public int maxDistance = 8;
private Sprite sprite;
public AudioClip swords;
public int life = 100;
public int points = 15;
private Transform myTransform;
IEnumerator Death() {
yield return new WaitForSeconds(0.8f);
Destroy(gameObject);
PointCounter.points += points;
}
void Awake() {
myTransform = transform;
sprite = (Sprite)GetComponent(typeof(Sprite));
}
void Start () {
GameObject go = GameObject.FindGameObjectWithTag("Player");
target = go.transform;
maxDistance = 8;
}
void FixedUpdate () {
if(Vector3.Distance(target.position, myTransform.position) > maxDistance) {
myTransform.position -= myTransform.right * moveSpeed * Time.deltaTime;{
sprite.Play ("EnemyWalk");
if (life <=0) {
sprite.Stop ();
}
}
}
}
//Health and damage rate when enemy is hit
void OnTriggerEnterSprite(SpriteCollider.CollisionData data) {
sprite.Play("hurt");
if(sprite.IsAnimationPlaying ("hurt")){
audio.Play();{
life += -100;
if (life <= 0) {
sprite.Play ("death");
StartCoroutine(Death());
}
}
}
}
}