- Home /
"SetDestination" can only be called on an active agent that has been placed on a NavMesh
So I'm making a 3d fps game. I was using @Brackeys tutorial - https://www.youtube.com/watch?v=xppompv1DBg just to add enemy. I checked my script 2 times, It's the same as in the video, i did all things correctly and got this error. I tried to press Window>Navigation and change some settings but it did not help me. Maybe this tutorial is not for 3D game, but i think it is. I'll be really thankful if someone will help. Thanks, here's my script public float lookRadius = 10f;
Transform target;
NavMeshAgent agent;
// Use this for initialization
void Start () {
target = PlayerMAnager.instance.player.transform;
agent = GetComponent<NavMeshAgent>();
}
// Update is called once per frame
void Update () {
float distance = Vector3.Distance(target.position, transform.position);
if (distance <= lookRadius)
{
agent.SetDestination(target.position);
if (distance <= agent.stoppingDistance)
{
FaceTarget();
}
}
}
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 * 5f);
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, lookRadius);
}
}
so, what's the problem? I'm not going through that tutorial.
Unity's Navmesh system is built for 3D games, so you're good. Two possible things that I see could be going wrong are: 1) You haven't baked a Navigation $$anonymous$$esh to the scene. This link should help if that's the case https://docs.unity3d.com/$$anonymous$$anual/nav-BuildingNav$$anonymous$$esh.html 2) You must either programmatically place the character on the Navmesh using a raycast or something, or simply drag the character close to the Navmesh. I hope this helps!
ok, first option helped me. Thanks a lot man :D
Answer by EddDom · Oct 25, 2020 at 05:51 PM
Well, you can go to Window > AI > Navigation and then configure which objects you want to be in the NavMesh (they have to be static). Then you can go to the Bake section of the tab, and click "Bake". Another thing is that make sure you place your player and enemies over the NavMesh. If they aren't, it will (probably) throw an error.
Another thing is you might want to change target = Player$$anonymous$$Anager.instance.player.transform; to target = Player$$anonymous$$anager.instance.player.transform;
Your answer
Follow this Question
Related Questions
Make an enemy follow along a wall 2 Answers
Tank not moving towards player 1 Answer