- Home /
NavMesh issue with spawning players
We use a script that says "take this objects transform, then randomly choose a position at 360 degrees and 100 units out, and spawn an enemy somewhere on the perimeter of that circle."
If I turn off the NavMesh and just run it, the script itself performs great. It will drop enemies randomly around that perimeter.
However, when I turn NavMesh on, they still spawn where they are supposed to, but some of them will "shoot" off to some other point of the map. Its almost like the NavMesh forces them off to some other "more suitable" point of the map. If I bake a mesh with 0 objects in the scene, and it is a perfectly solid blue plane, then they act fine. If I have a lot of objects in the scene and bake, then you get all the intricate geometry of the NavMesh. Which, seems to be the issue. The more complex, the more "dead" areas I have. Where now all the enemies actually span in the circle, but are immediately and very quickly shot over to some other point of the mesh that usually isn't inundated with these lines in the mesh.
To clarify, I know how to setup NavMesh parameters, and have tried an immense amount of variations. My actual ground in 100% flat through the whole thing. The only things I have really are height, to say what enemies can fit under, and radius, being how far around objects to avoid.
Do the spawned objects have script attached to them that control the Agent? If so, have you checked that you don't do any calculations on remainingDistance while no path has been assigned yet? I once saw a similiar issue and turned out that my programmer friend did some calculations before giving a destination and the agent bugged that way. I might be completely off, but i tought to share my experiences.
Answer by nution · Aug 18, 2014 at 11:53 PM
It turned out (for some odd reason) we had to turn off the NavMeshAgent in script, then turn it back on just after the character spawned. I also think this has something to do with the fact that the enemies are pooled, and just respawned over and over and never actually start the game instantiated and on the ground.
So, if you come across this issue, I would say to turn off the NavMeshAgent component by default, then when you spawn or respawn, activate it, when the character dies and is "destroyed" (more like deactivated) THEN turn it off, and keep it off till you instantiate him again in his new location. MAybe wait a half second after he spawns, then activate. This solved it for us.
That's good to know :) $$anonymous$$ark your own answer so other will know how to solve it :)
Answer by lazer · Mar 12, 2017 at 06:14 PM
Same problem; this solution worked for me. Would still like to know what the actual reason this works is though, i.e. if its a specific number of frames, wall time, moon phase, UT HQ coffee pot state, etc.
still running into this issue in 2017. This is a work-around perhaps an explanation from someone or a bug fix?
Thanks guys
Answer by nathanwick · Jan 12, 2019 at 07:29 PM
Still running into this issue in 2019 lol.
So was I but if you read lazers response it works perfectly. In the start function just disable and then re-enable the navmesh agent. Hope you get it working :D