Hi guys i create and Enemy AI script and it works fine, (i hv an enemy that is in inicial position nad if i go inside range he start follow me. if i go out of range he stop chase. when is not chasing i use idle animation. wt i want to know is how i can get it to return the inicial position if i get out of chase range and stay their in idle animation?) i try a lot of things but no one work if anyone can help i will appreciate.
~using UnityEngine;
using System.Collections;
public class Mob : MonoBehaviour
{
public float speed;
public float range;
public CharacterController controller;
public Transform player;
private Fighter opponent; //copy of fighter script
public LevelSystem playerLevel;
public double impactTime = 0.36;
private bool impacted;
public int experience;
public AnimationClip idle;
public AnimationClip run;
public AnimationClip die;
public AnimationClip attackClip;
public int maxHealth;
public int health;
public int damage;
private int stunTime; //time that enemy will be stunned
float chaseRange = 15.0f;
float distance;
// Use this for initialization
void Start ()
{
health = maxHealth;
opponent = player.GetComponent<Fighter>();
}
// Update is called once per frame
void Update ()
{
if (!isDead ())
{
if(stunTime<=0)
{
if (!inRange ())
{
chase ();
}
}
else
{
}
}
else
{
dieMethod();
}
}
void attack()
{
if (animation [attackClip.name].time > animation [attackClip.name].length * impactTime && !impacted && animation[attackClip.name].time<0.9*animation[attackClip.name].length)
{
opponent.getHit(damage);
impacted = true;
}
}
public void getHit(double damage)
{
health = health - (int)damage;
if (health < 0)
{
health = 0;
}
}
public void getStun(int seconds)
{
CancelInvoke ("stunCountDown");
stunTime = seconds;
InvokeRepeating ("stunCountDown", 0f, 1f);
}
void stunCountDown()
{
stunTime = stunTime - 1;
if (stunTime == 0)
{
CancelInvoke("stunCountDown");
}
}
bool inRange()
{
if (Vector3.Distance (transform.position, player.position) < range)
{
return true;
}
else
{
return false;
}
}
void chase()
{
distance = Vector3.Distance(player.position, transform.position);
if (distance <= chaseRange)
{
transform.LookAt (player.position);
controller.SimpleMove (transform.forward * speed);
animation.CrossFade (run.name);
}
else
{
animation.Play(idle.name);
}
}
void dieMethod()
{
animation.Play (die.name);
if (animation [die.name].time > animation [die.name].length * 0.9)
{
playerLevel.exp = playerLevel.exp + experience;
Destroy(gameObject);
}
}
bool isDead()
{
if (health <= 0)
{
return true;
}
else
{
return false;
}
}
void OnMouseOver()
{
player.GetComponent<Fighter> ().opponent = gameObject;
}
}