It pains me to see people using the unet proximity checking, it’s really quite bad unfortunately.
If you want something that performs well with thousands of objects I put up the implementation I use in a gist.
Couple of notes. FindOrCreate takes an id, you can have multiple proximity grids. The second argument ‘max’ just needs to be a big number, larger then the width of your area. Cell size is basically visibility range.
Because it works on nested grids proximity is not exact, but once you just have the objects near you according to the grid you can now filter on a more specific distance calculation if you need that, and it will be fairly cheap because it’s already bounded.
This implementation won’t work with UNet “out of the box”, however, I really appreciate you posting this. Can you add notes to the code to help explain the purpose/function of various aspects of the code?