- Home /
How do I get my enemy to attack?
I have tried several ways to get my enemy sprite to switch to his attack animation once the max distance is reached, however they aren't working. It always makes my enemy stay in the attack animation gliding towards me and once distance is reached he switches back to walk animation.
using UnityEngine;
using System.Collections;
using SpriteFactory;
public class EnemyAi : MonoBehaviour {
public Transform target;
public int moveSpeed;
public int rotationSpeed;
public int maxDistance = 6;
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);
}
void Awake() {
myTransform = transform;
sprite = (Sprite)GetComponent(typeof(Sprite));
}
void Start () {
GameObject go = GameObject.FindGameObjectWithTag("Player");
target = go.transform;
maxDistance = 6;
}
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 += -25;
if (life <= 0) {
PointCounter.points += points;
sprite.Play ("death");
StartCoroutine(Death());
}
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
enemy animations 1 Answer
hit delay does not kick in 0 Answers
Enemy animation help? 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How To Make Enemy Chase You? 1 Answer