- Home /
How do I make the enemy ai stop following the player in the scene properly on unity3d
I have the enemy ai following the play and I nav mesh set to stop in the inspector . The problem is the enemy ai is still following the player regardless if its dead or alive . I want the enemy ai to die where its at. I have it to where the enemy ai is checking to see if the player is near. Here is my script :
using UnityEngine;
using System.Collections;
public class Enemyai : MonoBehaviour {
public Transform player;
static Animator anim;
public NavMeshAgent nav;
void Start ()
{
anim = GetComponent<Animator> ();
}
void Update ()
{
if (Vector3.Distance(player.position, this.transform.position) < 13)
{
Vector3 direction = player.position - this.transform.position;
direction.y = 0;
this.transform.rotation = Quaternion.Slerp (this.transform.rotation,Quaternion.LookRotation(direction), 0.1f);
anim.SetBool("isIdle",false);
if(direction.magnitude > 2.6 )
{
nav = GetComponent <NavMeshAgent> ();
nav.SetDestination (player.position);
anim.SetBool("isMoving",true);
anim.SetBool("isAttack",false);
}
else
{
anim.SetBool("isAttack",true);
anim.SetBool("isMoving",false);
}
}
else
{
anim.SetBool("isIdle",true);
anim.SetBool("isMoving",false);
anim.SetBool("isAttack",false);
}
}
}
Hey mate ! I'm not familiar with navmeshes but I would recommend the following. First your need a way to declare your AI dead. For example, you can add a "$$anonymous$$illAI" method to your script and call it when your AI is getting shot by your player.
Then I would try two things to make your AI stop following your player :
First, you can try to disable the navmesh script.
Second, you can set the destination to your AI current position.
By the way, you might wanna disable the AI when your enemy dies (I suppose it makes no sense for you that some AI keeps being computed for dead enemies?)
Here is how you can achieve that :
using UnityEngine;
using System.Collections;
public class Enemyai : $$anonymous$$onoBehaviour {
public Transform player;
static Animator anim;
public Nav$$anonymous$$eshAgent nav;
// Call this method when your player shoot & kills your AI
public void $$anonymous$$illAI()
{
// 1st solution
nav.enabled = false;
// 2nd solution
nav.SetDestination (nav.position);
// If you want to stop the AI when the enemy dies
enabled = false;
}
void Start ()
{
anim = GetComponent<Animator> ();
}
void Update ()
{
if (Vector3.Distance(player.position, this.transform.position) < 13)
{
Vector3 direction = player.position - this.transform.position;
direction.y = 0;
this.transform.rotation = Quaternion.Slerp (this.transform.rotation,Quaternion.LookRotation(direction), 0.1f);
anim.SetBool("isIdle",false);
if(direction.magnitude > 2.6 )
{
nav = GetComponent <Nav$$anonymous$$eshAgent> ();
nav.SetDestination (player.position);
anim.SetBool("is$$anonymous$$oving",true);
anim.SetBool("isAttack",false);
}
else
{
anim.SetBool("isAttack",true);
anim.SetBool("is$$anonymous$$oving",false);
}
}
else
{
anim.SetBool("isIdle",true);
anim.SetBool("is$$anonymous$$oving",false);
anim.SetBool("isAttack",false);
}
}
}
I tried your script and I am getting errors.
Answer by rocket350 · Jul 27, 2016 at 11:38 AM
Ok, I misunderstood you in your previous question.
If you want your Enemy GameObject to continue existing even after death, but want it to stop moving, add the following function in you original EnemyAI script:
public void Die(){
//Disable all animations on the enemy AI
anim.SetBool("isIdle",true);
anim.SetBool("isMoving",false);
anim.SetBool("isAttack",false);
//Stop the navMeshAgent
nav.Stop();
//Remove the AI script (the enemy is dead now)
Destroy(this);
}
If you want to completely destroy the gameobject, add this function instead
public void Die(){
//Destroy the enemy gameobject
Destroy(gameObject);
}
In your enemy AI Health script you posted, add the following lines at the end of DieLocal()
GetComponent<Enemyai>().Die();
So this is what your Enemyai script would look like: using UnityEngine; using System.Collections;
public class Enemyai : $$anonymous$$onoBehaviour {
public Transform player;
static Animator anim;
public Nav$$anonymous$$eshAgent nav;
void Start ()
{
anim = GetComponent<Animator> ();
}
public void Die(){
//Disable all animations on the enemy AI
anim.SetBool("isIdle",true);
anim.SetBool("is$$anonymous$$oving",false);
anim.SetBool("isAttack",false);
//Stop the nav$$anonymous$$eshAgent
nav.Stop();
//Remove the AI script (the enemy is dead now)
Destroy(this);
}
void Update ()
{
if (Vector3.Distance(player.position, this.transform.position) < 13)
{
Vector3 direction = player.position - this.transform.position;
direction.y = 0;
this.transform.rotation = Quaternion.Slerp (this.transform.rotation,Quaternion.LookRotation(direction), 0.1f);
anim.SetBool("isIdle",false);
if(direction.magnitude > 2.6 )
{
nav = GetComponent <Nav$$anonymous$$eshAgent> ();
nav.SetDestination (player.position);
anim.SetBool("is$$anonymous$$oving",true);
anim.SetBool("isAttack",false);
}
else
{
anim.SetBool("isAttack",true);
anim.SetBool("is$$anonymous$$oving",false);
}
}
else
{
anim.SetBool("isIdle",true);
anim.SetBool("is$$anonymous$$oving",false);
anim.SetBool("isAttack",false);
}
}
}
And your DieLocal function in your AI Health Script
private void DieLocal(Vector3 position, Vector3 force) {
// Notify those interested.
EventHandler.ExecuteEvent(m_GameObject, "OnDeath");
EventHandler.ExecuteEvent<Vector3, Vector3>(m_GameObject, "OnDeathDetails", force, position);
// Spawn any objects on death, such as an explosion if the object is an explosive barrell.
for (int i = 0; i < m_SpawnedObjectsOnDeath.Length; ++i) {
ObjectPool.Instantiate(m_SpawnedObjectsOnDeath[i], transform.position, transform.rotation);
}
// Destroy any objects on death. The objects will be placed back in the object pool if they were created within it otherwise the object will be destroyed.
for (int i = 0; i < m_DestroyedObjectsOnDeath.Length; ++i) {
if (ObjectPool.SpawnedWithPool(m_DestroyedObjectsOnDeath[i])) {
ObjectPool.Destroy(m_DestroyedObjectsOnDeath[i]);
} else {
Object.Destroy(m_DestroyedObjectsOnDeath[i]);
}
}
// Deactivate the object if requested.
if (m_DeactivateOnDeath) {
Scheduler.Schedule(m_DeactivateOnDeathDelay, Deactivate);
GetComponent<AudioSource>().Play();
anim.SetTrigger("isDead");
}
GetComponent<Enemyai>().Die();
}
I can make the project into a zip file and download it and check it . I've gotten an error when I did your script :
private void DieLocal(Vector3 position, Vector3 force) {
// Notify those interested.
EventHandler.ExecuteEvent(m_GameObject, "OnDeath");
EventHandler.ExecuteEvent<Vector3, Vector3>(m_GameObject, "OnDeathDetails", force, position);
// Spawn any objects on death, such as an explosion if the object is an explosive barrell.
for (int i = 0; i < m_SpawnedObjectsOnDeath.Length; ++i) {
ObjectPool.Instantiate(m_SpawnedObjectsOnDeath[i], transform.position, transform.rotation);
}
// Destroy any objects on death. The objects will be placed back in the object pool if they were created within it otherwise the object will be destroyed.
for (int i = 0; i < m_DestroyedObjectsOnDeath.Length; ++i) {
if (ObjectPool.SpawnedWithPool(m_DestroyedObjectsOnDeath[i])) {
ObjectPool.Destroy(m_DestroyedObjectsOnDeath[i]);
} else {
Object.Destroy(m_DestroyedObjectsOnDeath[i]);
}
}
// Deactivate the object if requested.
if (m_DeactivateOnDeath) {
Scheduler.Schedule(m_DeactivateOnDeathDelay, Deactivate);
GetComponent<AudioSource>().Play();
anim.SetTrigger("isDead");
}
GetComponent<Enemyai>().Die();
}
Here is the link : http://www.mediafire.com/download/nrlf09jf4a8cezt/New+Unity+Project+1.zip
O$$anonymous$$ I downloaded the project. Your problem is a simple one: The Die() function is called Stop() ins$$anonymous$$d. So what you need to do is this: IN your EnemyAI script, rename Stop() to Die(). That's all!
PS: Nice scene!
Cheers :)
Your answer
![](https://koobas.hobune.stream/wayback/20220612085607im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
AI Enemy Follow Player 2 Answers
Enemy is not stopping following the player 2 Answers
Following AI - Similar to Snake Game 2 Answers
Make an NPC follow player? 1 Answer