I tried to use IEnumerator instead of FixedUpdate or Update but my FPS doesn’t improve much !
IEnumerator CheckUpdate(){
yield return new WaitForSeconds (0.05f);
if (healthBar.value <= 0|| checkRun == true) {
} else {
if (targets == null || targets.tag != ChaseEnemyTag)// if target death find another target
targets = GameObject.FindGameObjectWithTag (ChaseEnemyTag);
if (targets != null) {
direction = targets.transform.position - this.transform.position;
angle = Vector3.Angle (direction, this.transform.position);
float distance = Vector3.Distance (targets.transform.position, this.transform.position);
if (targets.transform.GetChild (0).name == "front") {
if ( distance < 20 && angle < 90 || distance < 10) {
if (z == 1) {
checkRun = true;
runAway ();
} else {
direction.y = 0;
this.transform.rotation = Quaternion.Slerp (this.transform.rotation, Quaternion.LookRotation (direction), 0.1f);
m_Anim.SetBool ("IsIdle", false);
m_Anim.SetBool ("IsRunning", false);
if (direction.magnitude > m_AttackRange) {// if target isn't in attack range move to front of target
m_Nav.Resume ();
m_Nav.SetDestination (targets.transform.GetChild (0).position);
m_Anim.SetBool ("IsWalking", true);
m_Anim.SetBool ("IsAttacking", false);
} else {
m_Nav.Stop ();
this.transform.rotation = Quaternion.Slerp (this.transform.rotation, Quaternion.LookRotation (direction), 0.1f);
m_Anim.SetBool ("IsWalking", false);
m_Anim.SetBool ("IsAttacking", true);
}
}
} else {
targets = null;
Patrol ();
}
} else {
targets = null;
Patrol ();
}
} else {
Patrol ();
}
}
StartCoroutine (CheckUpdate ());
}