Lag nearly game freeze after NavMeshAgent.Stop [Soved]

I got a small script for removing my monster the problem is when i execute it the game go down to 1 frame per minute. After some tests i found the problem it is line 4. where i Stop the NavMeshAgent when i remove it there is no game freeze but i need the agent to stop.
NavMeshAgent .speed = 0 also lead to a freeze i realy dont know what goes wrong…

Code

        if(alive == false && remove == false)
        {
            this.transform.FindChild("Model").GetComponent<SkinnedMeshRenderer>().enabled = false;
            this.GetComponent<NavMeshAgent>().Stop();

            Main.gold  +=  10 * Main.wave + 1;
            remove         =  true;
            remove_time =  Time.time + 5;

            if(this.GetComponent<MonsterDebuffs>().poison == true)
                this.GetComponent<MonsterDebuffs>().EndPoison();
            if(this.GetComponent<MonsterDebuffs>().slowed == true)
                this.GetComponent<MonsterDebuffs>().EndSlow();
            if(this.GetComponent<MonsterDebuffs>().stunned == true)
                this.GetComponent<MonsterDebuffs>().EndStun();
        }
        if(remove == true && Time.time >= remove_time)
        {
            Destroy(gameObject);
        }

so if you comment out that line of code, the problem goes away?

After some more testing it seems like if i do a NavMeshAgent.stop lag is there for 100% when i remove it i have a chance that there is no lag and after some time the game go back to normal fps

Edit : Ok got it now it have to do with the Agent Size Radius every monster with a Agent Size that is to small cause that freeze…

looks like every thing smaller then 0.01 can cause a freeze at 0.001 it is a 100% freeze :slight_smile:

Edit2: This also removed the lag that some time came up when instantiating a Monster…

Destroy(gameObject) will also cause the freeze when the size is 0.001

those sizes are very small.

any reason for such small radius?

At the moment i am to lazy to port my old A* from java or to write a new one in c# thtas the main reason using navmesh and those small values gave me the best results when the monster moved through a maze…

well, if its giving you problem, you might need to look at increasing the size of your maze. Not sure there is a solution for managing it otherwise.

You could also manage all the movement code yourself. You can generate paths without an agent.

NavMesh.CalculatePath

Sounds nice with this i probly only need to calculate one path every x minutes

also they say
Returns
bool True if a either a complete or partial path is found and false otherwise.

is there a easy way to get true only on a complete path not partial ?

the path status should indicate if its full or partial

Thank you for the help will try to implement it for now it sounds very promising

Ok it work as needed here a small test
Code

        NavMesh.CalculatePath (this.transform.position, waypoints [0].transform.position, NavMesh.AllAreas, path);
        if (path.status == NavMeshPathStatus.PathInvalid)
        {
            Debug.Log ("no path");
        } else
            this.GetComponent<NavMeshAgent> ().SetPath (path);

Well got it working now more or less the problem is that when i place my tower to close to the first spawn/way point
the agent lose his path.

Situation 1 tower are placed far away he get his path and start moving

Situation 2 tower are close to the spawn and first way point navmeshagent dont get his path or lose it i dont know

I also played with diferent nav mesh sizes and other values but dont got any progress

Where i get the Path from
Code

public class PathFinding
{
    private NavMeshPath []     path;
    public  GameObject[]     way_points;
    public  GameObject         spawn;
    private bool             blocking = false;

    public PathFinding ( GameObject[]     way_points, GameObject spawn)
    {
        this.way_points = way_points;
        this.spawn = spawn;
        this.path = new NavMeshPath[way_points.Length];
    }

    public void DrawDebugLine()
    {
        for(int i = 0; i < path.Length - 1; i ++)
            for (int c = 0; c < path[i].corners.Length-1; c++)
                Debug.DrawLine(new Vector3(path[i].corners[c].x,1,path[i].corners[c].z), new Vector3(path[i].corners[c+1].x,1,path[i].corners[c+1].z), new Color(0.0f,0.0f,0.0f),10,false);       
    }

    public bool CalculatePath()
    {
        path = new NavMeshPath[way_points.Length];
        blocking = false;
        for (int i = 0; i < way_points.Length-1; i ++)
        {           
            path[i] = new NavMeshPath();

            NavMesh.CalculatePath (way_points [i].transform.position, way_points [i+1].transform.position, NavMesh.AllAreas, path[i]);

            if (path[i].status != NavMeshPathStatus.PathComplete)
            {
                blocking = true;
            }
        }
        return blocking;
    }

    public NavMeshPath GetPath(int index)
    {
        return path [index];
    }
}

where i give it to the navmesh
Code

    void Start()
    {
        this.GetComponent<NavMeshAgent>().SetPath(Main.path_finding.GetPath (way_point));
        // when tower are to close Distance is 0
        Debug.Log (this.GetComponent<NavMeshAgent>().remainingDistance);
        for (int c = 0; c < this.GetComponent<NavMeshAgent>().path.corners.Length-1; c++)
            Debug.DrawLine(new Vector3(this.GetComponent<NavMeshAgent>().path.corners[c].x,2,this.GetComponent<NavMeshAgent>().path.corners[c].z), new Vector3(this.GetComponent<NavMeshAgent>().path.corners[c+1].x,2,this.GetComponent<NavMeshAgent>().path.corners[c+1].z), new Color(0.5f,0.5f,1.0f),5,false);       
   
        alive = true;
    }

Ok agen a stupid mistake i got 3 overlaping navigation meshes (forgot to remove the static modifyer when dublicating some assets)
after removing 2 of them no more problems.

for now … :stuck_out_tongue: