-
-
Save druggedhippo/76febb3e1a7375e49ff0180cebcbc0eb to your computer and use it in GitHub Desktop.
@shasaur Hi, this works by creating boxes every X units across the terrain.
If you used the default step size of "1", then Unity is trying to create 1million (1000x1000) boxes and doing raytraces on each, so Unity isn't very happy with you.
If you increase the step size to 100 then it'll finish in a minute or so, since then it's only making 1000 boxes, but then your nav resolution will be rather larger and not as fine as you may want.. You can also try step size of 10
There are clearly avenues for optimization here, grouping same texture areas together into a single larger volume to reduce the number of objects for example, but at the time this GIST served my purpose.
--
In the immediate now though, if you set step to 10 and comment out line 139 it won't actually DELETE the navigation volumnes it's using to create the nav hints, so you can then customize them, resize them, add your own to fix up those bits around your areas. Then you can use the "Bake" button on the "NaveMeshSurface" component and it'll use the boxes that are in place as it's navigation hints.
--
I also see I made a mistake at line 89, it should be:
objT.localScale = Vector3.one;
Wow that actually kinda worked, well-written!
Here's with a step size of 10:
And a step size of 5 (though takes significantly longer):
Even if they don't fully connect, I imagine I could use the step size 5 one because the agent will essentially interpolate the path between the squares which should look accurate enough.
@shasaur Hi, I've updated this gist with a newer version that is signifinicantly quicker than the previous one. A 1000x1000 grid is now generating under 10 seconds for me even at step 1. (I also added a timeout check so it should exit early instead of locking up unity)
Set your areas like before, but now also set the "DefaultArea" which is what the predominate Navigation Area will be, which will probably be the same as the firste area ID.
Under Advanced, I'm going to guess your NavMeshSurface agent setting "Default Area" is set to "Walkable" .
And, your first terrain area id is set to "Grassy", and you have "Grassy" in "Defaultarea". What this means in effect is that the generation code ignores it because it thinks it's the same as the NavMeshSurface agent, which means no modifier volume is created and Unity will automatically assign that location to "Walkable" when it builds the navmesh. This is at line 114, if the detected texture (Grassy) matches the specified "Defaultarea" value (in your case Grassy), it simply won't put a volume modifier, so it becomes Walkable by default.
Your Dirt_B is not being ignored, but it's being set to "Walkable", which the same as default NavMeshSurface agent setting, which is why your Grassy texture and Dirt_B textures are both having Walkable (blue) assigned to them.
The best way of fixing is is not to use Walkable in any of the other areas, use a different agent layer like "Path".
--
If you change the "Defaultarea" setting to Walkable, but then the script will try to generate a million checks again because the default texture isn't Walkable, it's Grass.
--
Alternately you can change the first Area ID to "Walkable", the Default area to "Walkable" and leave "Walkable" as the "Advanced -> Default Area" setting.
Yeah you're right, I think for some reason I thought I couldn't change the default area cost so I decided to have this work around. But I've changed to have the 'Walkable' area be the default as you suggested and adjusted the area costs and now it works perfectly!
Thanks for this!
I still need to mess around with the path thickness and probably use a different texture for grass detail shading (so I don't get the random squares), but this works very well! Excited to try this out with the NavMesh agents.
hi。i got a crash when generate navareas on Unity 2021.2.19f1. i think it happen because my terrain has no texture layer.
Worked great Thank You..
to those crashing Unity.. it happened to me a few times before I got the settings correct.
Area ID needs to be your Navmesh Layers (Names) corresponding to how they match to your Terrain layers. once I looked at shasaur image I realized my mistake and had it working. I'm trying to do this during runtime as I update connecting paths so I'm going try and spread the load out over a few seconds , as I don't need the navmesh update right away (as that wont be noticeable to players during the game) but don't want the 1-3 sec lag (Frozen screen) it takes to update. if I see a good improvement ill add it here..
Hey, I found this from this forum thread. I tried using it with my terrain but it took 20+ minutes to generate so I just interrupted it by closing Unity.
Do you think that's expected with my terrain size?