- Home /
Navmesh Agent Not placed on mesh?
Hi gang, I have Instantiated a navmesh Agent on a cube, both the Spawnpoint and the Baked Navmesh plane are at 0,0,0 so it should be spawning on the Navmeshplane but I'm getting the Notp laced on navmesh Error any way to fix this? "GetRemainingDistance" can only be called on an active agent that has been placed on a NavMesh. Everything is setup correctly.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using Photon;
public class PlayerMove : Photon.MonoBehaviour {
public float ShootDistance = 10;
public PlayerShooting PlayerShoot;
public NavMeshAgent Agent;
private Ray ShootingRay;
private RaycastHit shootHit;
public bool TeamOne;
public bool TeamTwo;
private bool enemyClicked;
// Use this for initialization
void Start () {
if (photonView.isMine == true) {
gameObject.tag = "MyPlayer";
Agent = gameObject.GetComponent<NavMeshAgent> ();
Agent.GetComponent<NavMeshAgent> ();
}
}
// Update is called once per frame
void Update () {
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Input.GetButtonDown ("Fire2")) {
if (Physics.Raycast (ray, out hit, 10000)) {
if (TeamOne == true) {
if (hit.collider.CompareTag ("TeamTwo")) {
enemyClicked = true;
Agent.Resume ();
}
}
if (TeamTwo == true) {
if (hit.collider.CompareTag ("TeamOne")) {
enemyClicked = true;
}
}
} else {
enemyClicked = false;
Agent.destination = hit.point;
Agent.Resume ();
}
}
if (Agent.remainingDistance <= Agent.stoppingDistance) {
Agent.Stop ();
} else {
Agent.Resume ();
}
if (enemyClicked == true && Agent.remainingDistance < ShootDistance) {
PlayerShoot.Shooting = true;
} else {
PlayerShoot.Shooting = false;
}
}
}
I'd like to add something unrelated. It looks like you're using Photon, and you don't check if you own the photonView in update. So everyone will be able to control anything with this script on it.
Answer by NorthStar79 · Dec 13, 2017 at 01:59 PM
this error usually appears when nav mesh agent is not placed on navmesh area. there are some mistakes that people do often when dealing with navmesh agents. i will try to sort some of them here, I hope this helps.
first of all, placing navmesh agent below navmesh level, this usually throws this error. to avoid this, place your navmesh agent lite bit higher than navmesh (just lite bit not too much nor same level)
second, changing game objects transform after initializing, if a game objects position changes via code navmesh agent may stuck old position, thus can cause this kind of error. to solve this I Suggest using NavMeshAgent.Warp instead of changing game objects transofm.position.
another cause of this error is , sometimes navmesh data can be corrupted (after updating unity for example) if this is a problem solution is re-baking nav mesh usually.
please check this steps and inform us if you get this error again. if your problem isn't one of the mistakes I pointed here, we will help you to investigate deeper.
Hi thanks you for replying, I have spent the last hour Trying all of these options after doing a google search for an answer. it's quiet strange I've also tried my spawnpoint for my player (testcube) at verious hights above the Plane and I have also tried resetting my PC and re-baking nothing seems to work.
its very strange, can we see screenshots from your scene,inspector and navigation settings?
and other codes that may influence navmesh agent.
oh, i need to say one more common mistake too , if you are loading your scene as additive and your player stays other scene, that will cause this error too. i was learned this one by hard way :)
I have a simple Login screen, then I load in my Player Home Base scene, and then I photon-instansiate my Players Ship. I have tried disabling ridgidbody act, but nothing seems to work... I have copied and pasted the click to move laern code to see if its my script but that to dosnt seem to work...
How you could validate When this is going to happen is there any flag that indicate when an active agent has not been placed on a Nav$$anonymous$$esh?
How you could validate When this is going to happen is there any flag that indicate when an active agent has not been placed on a Nav$$anonymous$$esh?
Answer by Unshackled · Jun 26, 2019 at 02:25 PM
I found a solution by adding a tag to the floor called "floor", then adding this to the bottom of the script:
public void OnTriggerEnter(Collider other)
{
if ((other.tag == "floor") && (isonfloor == false))
{
agent.enabled = false;
agent.enabled = true;
isonfloor=true;
}
}
It seems to reset them if they have left the ground (like when you fly-kick them).
Answer by toyhunter · Sep 02, 2021 at 06:45 AM
The error "XXX can only be called on an active agent that has been placed on a NavMesh" occurred even if your setting are 100% correct but somehow your baked navMesh was gone. What you have to do is bake that mesh again, cheers!
Your answer
Follow this Question
Related Questions
Random Position on Nav Mesh 1 Answer
Navmesh and multiples floors 2 Answers
Nav Mesh Agent Type 3 Answers
Agent is going through walls set as non walkable 1 Answer
NavMesh does not working... 0 Answers