I have a nav mesh attach to the enemy ai . The problem is when the player enter the nav mesh agent radius and enemy ai stays on the player and it hard for the player to get away from the enemy ai . When I change radius to a much larger enemy ai moves all over the place , I do have a rigidbody attach to the enemy ai . Is their any way of getting around this ?
I have a video made on the problem
Here is my script :
using UnityEngine;
using System.Collections;
public class Enemyai : MonoBehaviour {
public Transform Player;
Animator anim;
public NavMeshAgent nav;
void Start ()
{
anim = GetComponent ();
}
public void Stop(){
anim.SetBool("isMove", false);
anim.SetBool("isAttacking", false);
anim.SetBool("isIdling", true);
this.enabled = false;
//Or if you want to destroy the AI script completely
//Destroy(this)
}
void Update ()
{
float speed = 0.9f;
if (Vector3.Distance(Player.position, this.transform.position) < 10000f)
{
nav = GetComponent ();
nav.SetDestination (Player.position);
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("isIdling",false);
if(direction.magnitude > 2 )
{
this.transform.Translate(0,0,0.15f);
anim.SetBool("isMove",true);
anim.SetBool("isAttacking",false);
}
else
{
anim.SetBool("isAttacking",true);
anim.SetBool("isMove",false);
}
}
else
{
anim.SetBool("isIdling",true);
anim.SetBool("isMove",false);
anim.SetBool("isAttacking",false);
}
}
}
↧
Nav Mesh agent problem / Player can not get away from enemy ai nav mesh agent attach for some reason
↧