How to avoid randomly generated chunks overlapping

Hi there!

Still working on my first project: an endless runner 2D game. At the moment I have a system where whenever a random chunk is generated, it also has a level generator which, based on the distance of the player, can generate the next random chunk on the endpoint of the current chunk.

Problem is that these chunks sometimes overlap, I want to avoid this. Does anyone have any ideas what the best way of doing this is?

I was thinking about using a box collidor and OnTriggerEnter to destroy the chunk when there is an overlap and generate a new random chunk (which would do the same overlap check, until a random chunk is generated that does not overlap). However I don’t think the OnTriggerEnter works with static objects generated within the box collider, or at least it’s not doing anything for me at the moment.

I was also looking at maybe something like Physcis.Overlap, but no idea how I should make use of that.

Maybe there are easiers ways of doing this, like doing a check before generating a new chunk.

Using colliders and OnTriggerEnter after the fact seems like bad idea. Knowing the size of each chunk and taking into account when generating next one is the way to go. The simplest thing is choosing some size and having all chunks same size. Also design your chunks with tiling in mind in mind. Make sure that the edges of chunks fit together.

Instead of fixed chunk size you could also choose “tile size” and have each chunk multiple of the tile size. When I am talking about tile size it doesn’t mean your game needs to use tilemap renderer. Just a way of organizing objects/chunks and their sizes so that they can easily fit together.