EnemyAI movement stop if it has enable animator controller
I'm making enemy ai for my fps game. I create a cube and attach animator controller and NaveMeshAgent to ti. And i make simple enemy AI script. I'm using NaveMeshAgent, All thing work great until if i don't enable animator controller. This is my enemy AI script.
using UnityEngine; using UnityEngine.AI; using System.Collections;
public class EnemyAI : MonoBehaviour {
public float eHealth = 100f;
public bool isDead;
Transform target;
NavMeshAgent nav;
Animator anim;
void Start()
{
nav = GetComponent<NavMeshAgent> ();
anim = GetComponent<Animator> ();
target = GameObject.FindGameObjectWithTag ("Player").transform;
isDead = false;
}
void Update()
{
if (eHealth < 0.1) {
anim.SetBool("Dead", true);
Destroy(gameObject, 3);
isDead = true;
}
nav.SetDestination (target.position);
}
}
Answer by tormentoarmagedoom · Jun 19, 2018 at 10:10 AM
Good day.
If your animation controls the position of the object, i supose the navmesh agent will go crazy... If you need to animate position and use navmeshagent, dont do it in the same object.
I better to have a empty parent with the NavMeshAgent, and a child with the animation.
Bye!
Thank you for replay sir, It's working now but it's create a new problem for me. if i attack rigidbody to my enemy ai. its moving but dead animation is stuck not working properly. when enemy dead, its goinng to y axis. but if i remove rigidbody and navmeshagent. its all working fine!
using UnityEngine; using UnityEngine.AI; using System.Collections;
public class EnemyAI : $$anonymous$$onoBehaviour {
public float eHealth = 100f;
public bool isDead;
Transform target;
Nav$$anonymous$$eshAgent nav;
Animator anim;
void Start()
{
nav = GetComponent<Nav$$anonymous$$eshAgent> ();
anim = GetComponent<Animator> ();
target = GameObject.FindGameObjectWithTag ("Player").transform;
isDead = false;
}
void OnTriggerStay (Collider col)
{
if (col.gameObject.tag == "Player") {
anim.SetBool ("Attack", true);
FpsController player = col.gameObject.GetComponent<FpsController>();
player.pHealth -= 0.1f;
}
}
void OnTriggerExit (Collider col)
{
if (col.gameObject.tag == "Player") {
anim.SetBool ("Attack", false);
FpsController player = col.gameObject.GetComponent<FpsController>();
player.pHealth -= 0;
}
}
void Update()
{
if (eHealth < 0.1) {
isDead = true;
Destroy (gameObject);
}
if (isDead == false) {
nav.SetDestination (target.position);
}
}
}
Good day. I dont get exatcly what you say is happening.
But i recommend you to have all components (colliders, scripts, Rigidbody, etc...) except for animation in an empty gameobject that contains a child. In this child have the $$anonymous$$esh and the animation.