So I’m working on a little tower defense project which uses a NavMesh for Enemy Pathfinding. I got NavMesh Pathfinding to work.
Everything worked fine for a long time. Then, however, at some point the NavMeshAgents stopped being able to find a path to the target, resulting in them not moving.
I didn’t change anything in the Scene, the NavMesh or the corresponding scripts and honestly don’t know what could’ve caused this issue.
Here you can see my NavMesh. Enemies are supposed to move from one sphere to another, but instead they just keep standing at their SpawnPoints. The enemies spawn at the bottom left sphere and should move to the bottom right sphere
Here you can see the Enemies and Environment being selected
In my Code I have a reference on the target and spawnpoint sphere and my GameMaster script simply creates enemies at the spawnpoint sphere at sets their navmeshagent destination to the target sphere. As I mentioned above everything worked fine before until it just stopped working.
Do you have any ideas what could’ve caused this?
Ty in advance!
P.S.: Im using the additional NavMesh Components provided by Unity to bake a NavMesh and limit it with NavMeshObstacles
Two things. First, are they being placed (almost) directly onto the NavMesh, or are they being allowed to fall first? This can cause issues if they fall, since sometimes they won’t immediately get a reference to the NavMesh. (Keep in mind I haven’t messed about with NavMeshes and NavMeshAgents since Unity 4). Also, is the script setting their NavMeshAgent targets before they’re on the NavMesh? Since I could see this causing problems when it tries to consult the (non-existing) NavMesh and then just complains.
So, please don’t ask me HOW I came up with this but apparently there is a workaround to this problem.
(I don’t think it fixes the specific error, if I should run into anything similar I will probably post here again)
How my pipeline works: GameMaster creates an unitialized Enemy either via Instantiation or Resetting the values of a pooled enemy. Upon the first step for each enemy the Enemy Component System activates the GameMasters Init Process for that certain Entity (im using Hybrid ECS for the enemies). During this initialization the GameMaster calculates the path the Enemy will have to follow.
However, at some point the GameMaster randomly stopped calculating the path for each enemy, leaving every created enemy with a path of length 0 (and 0 corners, etc… basicly an empty path).
The Workaround: I moved the calculation into the Enemies’ Component System AND delayed the actual pathfinding by 1 Frame after the initialization of the enemy.
My conclusion would be that the problem lies within the NavMeshAgent itself. It might be an Unity Bug or I might have overlooked something. I’d guess the NavMeshAgent has trouble initializing itself AND calculating a path within 1 frame, but I’m not sure.