- Home /
Question by
nasby321 · Sep 05, 2013 at 02:33 AM ·
ai2d-platformerenemywalking
Stopping AI's animation and movement
I want the AI to stop doing his walking animation once he reaches the player and once death animation to play for him to stop following me. I tried to do it here:
myTransform.position -= myTransform.right * moveSpeed * Time.deltaTime;{
sprite.Play ("EnemyWalk");
if (life <=0){ sprite.Stop();
}
This stops the walk animation, but not the fact he still follows me during the yield seconds until his object is destroyed.
Here is my script:
using UnityEngine;
using System.Collections;
using SpriteFactory;
public class EnemyAi : MonoBehaviour {
public Transform target;
public int moveSpeed;
public int rotationSpeed;
public int maxDistance;
private Sprite sprite;
public AudioClip swords;
public int life = 100;
private Transform myTransform;
IEnumerator Death() {
yield return new WaitForSeconds(1.2f);
Destroy(gameObject);
}
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) ;
}
}
}
//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) {
sprite.Play ("death");
StartCoroutine(Death());
}
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Enemy AI With changing Player 0 Answers
How do you move did with [iTWEEN] the enemy has a component of [Character Controller]? 0 Answers
Need help with enemy AI 1 Answer
AI Enemy script 1 Answer
Enemy Attraction/Aggro 1 Answer