- Home /
How do I stop the enemy ai from following the player after it dies on unity3d properly in C#
When I shoot the enemy ai the animation plays . But the problem is that the enemy ai follow the player after it dies . Here is the move and attack 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);
}
}
}
Your issue is not clear yet. But I think that you are not destroying the enemy after it dies. Destroy(gameobject);
Or if you need the dead enemy-ai to be in the screen, and it should not follow, then just off the script of the enemy which is used to follow the player. Or turn off the nav$$anonymous$$esh of the enemy-ai. You could use GetComponent for this
Hope this will help you.