Unity Navigation stopping distance irregularity
http://www.mediafire.com/watch/logqpz9t49v8pct/UnityNavMeshError.mp4 ^(A video of my problem)
#pragma strict
var agent: NavMeshAgent;
var gos1 : GameObject;
var gos2 : GameObject;
function Start () {
}
function Update () {
// Find all game objects with tag Enemy
var gos : GameObject[];
gos = GameObject.FindGameObjectsWithTag("Enemy");
var closest : GameObject;
// Iterate through them and find the closest one
for (var go : GameObject in gos) {
if(gos1 == null){
gos1 = go;
} else if(gos2 == null && gos1 != go){
gos2 = go;
var distance1 = Vector3.Distance(gos1.transform.position, transform.position);
var distance2 = Vector3.Distance(gos2.transform.position, transform.position);
if(distance1 > distance2){
gos1 = gos2;
gos2 = null;
}
gos2 = null;
}
}
agent.SetDestination(gos1.transform.position);
}
Hey guys! I'm just trying to make a tower defence game not unlike boom beach by Supercell, but I'm having a problem with the Navigation! For some reason, the players(Navigation Objects) are moving to weird places on the objects e.g. the left side of a cube etc.
The code included is the script attached to the players(Navigation Objects) Thanks!
$$anonymous$$ay its a problem with your navmesh. Some of your agents, doesen't seem to move at all. $$anonymous$$ay they cant find a path and thats the reason why? There is definetly no problem in your code, besides it's a bit clunky.
I've tryed different bases(cubes used as floors) with other cubes on top, same thing. What do you suggest?
I could provide you with a picture of the navmesh tomorrow, will that help?
Could you try out this code? Its does basicly the same, but works different. Its written in C#, when thats a problem. private GameObject currentDest; public Nav$$anonymous$$eshAgent agent;
void Update()
{
GameObject[] allEnemys = GameObject.FindGameObjectsWithTag("Enemy");
if (allEnemys.Length == 0)
return; // There is no enemy
currentDest = allEnemys[0];
float bestDistance = Vector3.Distance(transform.position, currentDest.transform.position);
for (int i = 1; i<allEnemys.Length; i++)
{
if (bestDistance > Vector3.Distance(transform.position, allEnemys[i].transform.position))
{
currentDest = allEnemys[i];
bestDistance = Vector3.Distance(transform.position, allEnemys[i].transform.position);
}
}
agent.SetDestination(currentDest.transform.position);
}
Yes, I did think about the code as a problem, so I tryed just a simple setdestination just to the shere, same thing happened.
Then it must be a problem with your navmesh! The picture would help greatly.
O$$anonymous$$, I'll send you a picture ASAP, I'm away from my computer for a day so I'll reply a link to this page!
Here is the Nav$$anonymous$$esh Image: http://www.mediafire.com/view/hxdv73668yj1s8m/Nav$$anonymous$$esh.PNG
Could you debug this line like this. If the output is false, then the point the agents wants to move to, is not on the navmesh. Debug.log(agent.SetDestination(currentDest.transform.position));
Answer by Oribow · Oct 03, 2015 at 04:57 PM
Ok, I now know your problem: There isn't one, at all. It's just how the system works. The stopdistance on your agent kicks in, if the remaining path lenght is smaller than the stop distance. But the remaining path lenght isn't nescesary the same as the direct air distance between your agent and your Object. (look at the Screenshot). This code is a work around that. Just set the stop distance on all of your agents to zero. The script will stop the agent, when the direct air distance is smaller, than "minimumDistance"
#pragma strict
var agent: NavMeshAgent;
var gos1 : GameObject;
var gos2 : GameObject;
var minimumDistance : float;
function Start () {
// Find all game objects with tag Enemy
var gos : GameObject[];
gos = GameObject.FindGameObjectsWithTag("Enemy");
var closest : GameObject;
// Iterate through them and find the closest one
for (var go : GameObject in gos) {
if(gos1 == null){
gos1 = go;
} else if(gos2 == null && gos1 != go){
gos2 = go;
var distance1 = Vector3.Distance(gos1.transform.position, transform.position);
var distance2 = Vector3.Distance(gos2.transform.position, transform.position);
if(distance1 > distance2){
gos1 = gos2;
gos2 = null;
}
gos2 = null;
}
}
agent.SetDestination(gos1.transform.position);
}
function Update () {
if(Vector3.Distance(gos1.transform.position, agent.transform.position) < minimumDistance)
agent.Stop();
}
5 years later...yeah, this looks right. I think most of us interpret stopping distance as a "ring" around the destination, and that the agent should try to land right on the edge of the ring, when in fact it's more of a hint to the agent how close it needs to be to the destination. That is, if the agent is already within stopping distance of the destination, it shouldn't try to get any closer.
Also, in Unity 2019, it should be agent.isStopped = true
.