- Home /
Question by
importguru88 · Aug 18, 2016 at 07:42 PM ·
navmeshnav meshobstacle avoidance
How do I enable the Nav Mesh obstacles on enemy ai in my scene for avoidance in the scene
I need to enable the nav mesh obstacles on my enemy ai and set the destination in the script as well . I declare nav mesh as a variable already . I need help with the rest . Here is my script :
using UnityEngine;
using System.Collections;
public class Enemyai : MonoBehaviour {
public Transform Player;
Animator anim;
public NavMeshObstacle nav;
void Start ()
{
anim = GetComponent<Animator> ();
}
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 <NavMeshObstacle> ();
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);
}
}
}
Comment
Answer by anasalasel · Dec 27, 2020 at 05:59 PM
you need to add using UnityEngine.AI; in top of the script
Your answer
Follow this Question
Related Questions
Toggling NavMeshAgent and NavMeshObstacle 2 Answers
Unity NavMesh obstacle avoidance? 0 Answers
Problem with Navigation tab 0 Answers
NavMeshAgent and NavMeshObstacle together, unit jumping a small distance on toggle 2 Answers
Navmesh carving causing issues upon enabling navmesh agent component 0 Answers