Best Way to Group Objects into "Chunks" based on their position?

I am trying to make a system that groups script objects into ‘chunks’ based on the positions. I thought about using a Dictionary where the key is a Vector2 and the value is a list of scripts. I want something like this because getting nearby scripts would become very fast.

Unfortunately, I see a big problem with this method: I would need to update the objects every frame (Check if they moved a chunk. Delete them from the previous chunk. Add them to the next chunk).

Is there a more efficient way to do this wile still using a hashset-based system like Dictionaries? If not, how can I optimize this method?

You should research what is called an “octree”.

1 Like

Ah yes, thanks for reminding me. I have implemented that into my game and I see a lot of improvement. Thanks!

You could search for “spatial hash grid” or “hash grid” on google.