Why doesn't Recast graph generation support 2D colliders? Is there a way to submit changes if I get it working locally?


I’m finally trying to incorporate pathfinding into my top down 2D game, and I was hoping to use the recast graph to auto-generate a nav mesh after generating the level.

However, it seems like the recast graph generation does not support 2D colliders, instead relying on 3D terrain/colliders being present.

Is there any particular context on this? I’ve been digging into the code, and it looks like I could add a 2D collider generation step, but I’m also new enough to how this all works that I’m hesitant to start hacking things right away. :confused:

Additionally, if I do get 2D collider-based graph generation working… is there a way to propose that change to the core codebase? It seems simple enough and I can’t be the only person working on a 2D game with 2D level geometry

1 Like

Okay, so I’ve so far gotten basic 2D mesh generation working - basically, I can generate 3D meshes for the 2D colliders in the RecastMeshGatherer (and I added a “generate big 2D plane” phase so there’s no need for an overall plane mesh for the “ground”)

This seems like fairly viable mainline code - I can have this be a local hack for my version, but is there a way to like, upload my changes for consideration? :grimacing:

I’ve managed to get most 2D collider types working very straightforwardly (there’s an issue where it doesn’t “remove” the internal sections of their nav mesh, but A) I’m pretty sure someone more familiar with the asset could manage to fix that, and B) it doesn’t seem to cause any major problems since the different map sections seem actually separate.

Well, I think I’m done with my edits?

I can get the recast graph to generate with 2D colliders:

  • Box
  • Circle
  • Capsule
  • Polygon
  • Edge (though I skipped accounting for radius)

It all seems to work fine? Idk if there are any issues that somebody with deeper knowledge of the codebase would find though.

I’ve put together a Zip file containing my edited files: AStar Project 2D Recast Edits.zip - Google Drive

It’s really not that much:

  • [Generators/]
    • RecastGenerator - now has “use 2D colliders” bool and “generate 2D ground” controls
    • [Utilities/]
      • MeshUtils (new) - splits out 3D mesh generation utilities (ex: capsule cache, static Box mesh data) and reworks them a bit
      • MeshUtils2D (new) - extends the core MeshUtils and creates a bunch of 2D mesh generation code in a hopefully-readable way
      • RecastMeshGatherer - now uses the above two files and exposes functions to build 2D collider meshes and incorporate into the recast graph

(and then the 2D controls are exposed in the editor)


Thanks! That’s really cool!

The recast graph generation has changed a lot in the beta, but it might be possible to add in your code.
Some changes might be necessary, though. To make sure colliders don’t have holes in them.

Hey, thanks for responding!

Honestly that’s kinda why I was hoping to get these changes pushed - my current project is a bit longer-running and I’d ideally be able to use updated packages without breaking everything :sweat_smile:

Would you recommend I switch to the beta version?

Also, what do you mean by the colliders not having holes? The way the meshes are generated here is that it basically builds “walls” along the edges of the 2D colliders and expects that to cause cuts into the nav mesh. I think that should properly work with holes? :thinking: or is there some relevant change in the beta that makes holes more significant?

The beta has a lot more features, is faster, and may even be more stable. Up to you.

I mean that typically one would expect there to not be a navmesh inside the colliders.

1 Like


I got busy with other game stuff and have been working on this again - in anticipation that the answer to this issue might just be “move to the beta” I’ve been trying to port these changes to work in the beta version (which mainly boils down to copying them into the burst recast mesh gatherer).

I’ve gotten it nearly fully working (and it now fixes the holes issue) BUT I’m seeing a weird issue that I don’t understand - meshes are only getting intersected with the graph in the central “square” region?

(Yellow lines are me previewing the full transformed generated mesh data from the 2D colliders)

This seems like it’s almost certainly a bounding box error, but I don’t think it’s on the 2D mesh side and am not sure if there’s some internal thing I need to account for :confused:

I was hoping somebody with more knowledge of the plugin’s inner workings could help get this over the finish line :sweat_smile: I attached a unitypackage with the files I’ve changed (there’s really not that much code) - does anything look wrong?

Unitypackage download link

Okay, I got it working!

Basically, since I was computing a fresh bounds anyway I relied on setting the GatheredMesh bounds to new Bounds(), and then downstream logic recomputes it from scratch. This fixed the issue!

Updated package: astar_2d_changes_for_beta.unitypackage - Google Drive

Is there any chance this can be incorporated into the beta? :grimacing: I know that’s a lot to ask, but my project really requires using 2D colliders, and relying on the normal package download for the beta means I can’t easily edit things - and I’m hesitant to make such a critical piece of game infrastructure a hacked-in modded thing going forward :confused:


Any change you have a diff of the changes instead of a UnityPackage?

1 Like

Ah sorry - I didn’t set up Git for the project I did this in

The package only contains the 7 files I changed! so if you’re using version control on your end, you should be able to package it in and spot-check any accidental overwrites :frowning:

I’d really love for this to be added. I have to admit I am a bit disappointed in the lack of current support for graphs with 2D physics (especially large ones). This would help a ton.

Unfortunately I’m getting some errors when I try to import your package.

Import Error Code:(4)
Message: Build asset version error: packages/com.arongranberg.astar/generators/utilities/recastmeshgathererburst.cs in SourceAssetDB has modification time of ‘2023-08-29T10:47:30Z’ while content on disk has modification time of ‘2023-08-11T07:55:39.618Z’


The package cache was invalidated and rebuilt because the following immutable asset(s) were unexpectedly altered:

I assume it’s because I have A* as a scoped registry, and I’m unable to modify it?

Yep, I don’t think you can edit normal imported packages.

To make these edits, I manually copied the beta package into the project’s Assets directory instead of the readonly package, and the uploaded unitypackage is my edit on those files.

I was hoping to get the changes incorporated into the mainline beta so I can update to the next full version without constantly repeating these hacked-in changes :sweat_smile:

Ooof ya, that’s a bit rough.

Mr. Aron, please give us 2D people some love :stuck_out_tongue:

How exactly are you copying it into your Assets folder? When I do that I get a storm of errors.

Copied from packages to a different directory, then removed the package, then copied the files back into the Assets directory.

There were some errors to address but nothing too major - but this was also in a clean project with nothing else in it.

To migrate my project from the previous full release to the hacked beta was a lot more painful, but basically I removed all the old pathfinding code (leaving the project broken) then copied over the hacked beta files into my main project’s Assets/Plugins directory (and then spot checked a bunch of issues).

That all said: If this is adoptable into the mainline beta, I would definitely wait for Aron to incorporate it before tackling these hacks, since you’d need to undo all of this once the beta contains the changes you need.

I got to the point where the only error is:

Assets\A_PathfindingProject\Generators\RecastGenerator.cs(1803,23): error CS0122: ‘JobBuildRegions’ is inaccessible due to its protection level.

Which appeared after installing the UnityPackage.

Edit: Nah never mind this is super scuffed. Damn.

Wait really? I don’t think I changed anything relating to that :grimacing:

What beta version did you try to patch the changes into?

Project 4.3.79 (Beta)