Unity Navigation stopping distance irregularity

^(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!

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();
   
}