- Home /
Question by
Willrichplaysguitar · Apr 11 at 01:24 PM ·
navmeshagent
Nav Mesh Agent Error
I'm getting the error: SetDestination
can only be called on an active agent that has been placed on a NavMesh
.
UnityEngine.AI.NavMeshAgent:SetDestination (UnityEngine.Vector3)
My agent has already been created including my navmesh. I have checked the code and it looks fine to me. Wondering if its a bug with capsule collider/nav mesh component values?
Scripting below
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class EnemyAI : MonoBehaviour
{
[SerializeField] Transform target;
[SerializeField] float chaseRange = 5f;
[SerializeField] float turnSpeed = 3f;
EnemyHealth enemyHealth;
float distanceToTarget = Mathf.Infinity;
NavMeshAgent agent;
bool isProvoked;
void Start ()
{
agent = GetComponent<NavMeshAgent>();
enemyHealth = GetComponent<EnemyHealth>();
}
void Update ()
{
if (enemyHealth.IsDead())
{
enabled = false;
agent.enabled = false;
}
distanceToTarget = Vector3.Distance(target.position, transform.position);
if (isProvoked)
EngageTarget();
else if (distanceToTarget <= chaseRange)
isProvoked = true;
}
public void OnDamageTaken ()
{
isProvoked = true;
}
void EngageTarget ()
{
FaceTarget();
if (distanceToTarget >= agent.stoppingDistance)
ChaseTarget();
if (distanceToTarget <= agent.stoppingDistance)
AttackTarget();
}
void ChaseTarget ()
{
GetComponent<Animator>().SetBool("attack", false);
GetComponent<Animator>().SetTrigger("move");
agent.SetDestination(target.position);
}
void AttackTarget ()
{
GetComponent<Animator>().SetBool("attack", true);
print("Attacking Target");
}
private void FaceTarget ()
{
Vector3 direction = (target.position - transform.position).normalized;
Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * turnSpeed);
}
private void OnDrawGizmos ()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, chaseRange);
}
}
Comment
Your answer

Follow this Question
Related Questions
Navmesh orthogonal movement 0 Answers
NavMesh problem it does not respond corectly 0 Answers
My characther turning the wrong way when walking using Nav Mesh Agent 0 Answers
line in navmesh obstacle 1 Answer