Hello, im trying to make that the enemy is following the player with a script and a Nav mesh agent but its not working i got this error:
SetDestination" can only be called on an active agent that has been placed on a NavMesh.
UnityEngine.AI.NavMeshAgent:SetDestination(Vector3)
EnnemyController:Update() (at Assets/Script/EnnemyController.cs:23)
My script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class EnnemyController : MonoBehaviour {
public float lookRadius = 10f;
Transform target;
NavMeshAgent agent;
void Start () {
target = PlayerManager.instance.player.transform;
agent = GetComponent<NavMeshAgent>();
}
void Update () {
float distance = Vector3.Distance(target.position, transform.position);
if (distance <= lookRadius)
{
agent.SetDestination(target.position);
}
}
void OnDrawGizmosSelected ()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, lookRadius);
}
}
Iām still new to Unity, what else can I do to solve these kinds of problem? I tried doing the Window, AI,ā¦ solution but it did nothing. I also tried moving the NavMeshAgent up/down in the inspector menu but it shows a window that says āCannot restructure prefab instanceā : āChildren of Prefab instance cannot beā¦ā When I clicked Open Prefab, The inspector only showed the Transform and Animator. It did not show the NavMeshAgent nor the Script. Please help me
For me, it solved by this:
Maybe the person is a prefab and you are instantiating at runtime. This way, Reset the Transform of the prefab in such a way that he is touching the ground. Whether or not a person is a prefab, His X,Y,Z of transform should be in such a way that his feet touching the ground.
Your objectās capsule colliderās radius / height and the navmesh agentās colliderās radius / height has to match.
For example my enemyā s capsule colliderās radius was 39 and height was 223. Nav Mesh Agentās radius was 0,5 and height was 2,0 so always got an error such as SetDestination" can only be called on an active agent that has been placed on a NavMesh. After i changed nav mesh agentās radius and height to my enemy objectā s collider values, everything worked perfectly.
Thanks for the fix. I started with a modular scene and the instantiate to spawn points worked fine.(2018.4.13f1) Once I changed to a terrain, the spawn still worked but the enemies could not see the player and did not move. The fix was to add the prefabs back to the new terrain and create new prefabs (deleted the original prefabs). Of course I had to attach the new prefabs to the object references in the inspector since using [serialized fields].
That was our problem.
Any specific steps to take to fix this?
I havenāt been able to figure out how to create separate NavMesh in one scene? That was our original goal. It used to be done using the āNavMeshSurfaceā component on an Empty GameObject. You could have two or three of these in your scene.
Now though, it is a Navigation panel that seems to be one single NavMesh for the entire scene. Yet that means only one type of enemy can use the NavMesh and the others say there is no NavMesh at all.
I have done everything listed above but it still does not work, I have the same script as Tornado77, I have a NavMesh set on the floor of my scene, my character is touching the ground, my capsule colliderās radius is the same as the height of my navmesh agentās colliderās radius. What can Id do to fix this? (I also have the error: āFailed to create agent because it is not close enough to the NavMeshā)