Search code examples
c#unity-game-enginenavmesh

Unity3d NavMesh is working strange, can't understand why


The first wave of green goes right (to the first waypoint), but after lengthening the tunnel, the second wave is why green you lose the first waypoint and go straight to the second. (And why is that somehow a roundabout way)

Sorry for my bad english.

The first wave of green goes right (to the first waypoint), but after lengthening the tunnel, the second wave is why green you lose the first waypoint and go straight to the second. (And why is that somehow a roundabout way)

Actually two questions: 1) how to fix the first waypoint 2) why is it so weird going to the second waypoint

Here is the code of the enemy to iterate through waypoints.

using System;
using UnityEngine;
using UnityEngine.AI;

[RequireComponent(typeof(NavMeshAgent))]
public class EnemyMovement : MonoBehaviour
{
    [SerializeField] public Transform[] points;
    [SerializeField] private int destPoint = 0;
    private NavMeshAgent agent;

    void Start()
    {        
        agent = GetComponent<NavMeshAgent>();
        agent.autoBraking = false;
        agent.destination = points[destPoint].position;
    }

    void GotoNextPoint()
    {        
        if(destPoint != points.Length)
        {
            agent.destination = points[destPoint].position;
        }
    }

    void Update()
    {
        if(agent.remainingDistance < 0.5f)
        {
            destPoint++;
            GotoNextPoint();
        }
    }

    private void OnDrawGizmos()
    {
        Gizmos.DrawLine(gameObject.transform.position, points[destPoint].position);
    }
}

Solution

  • It’s decided that the thing is that NavMesh makes large tiles (NavMeshBuildSettings.tileSize), but I couldn’t change it because I used someone else’s work (https://github.com/Unity-Technologies/NavMeshComponents/tree/mast… mples / Scripts). So it turned out that to change the runtime navMesh, you must not only register in the grid change code, but write the line (.overrideTileSize = true;)

    var defaultBuildSettings = NavMesh.GetSettingsByID (0).overrideTileSize = true;
    

    After that I was able to change the size of the tile, and the choice of the wrong path was stopped.