- Home /
Infinite world generation help C# (Like chunks)
Okay, so I have a very simple problem that I have no idea how to approach. I'm making a 2d Topdown game and I would like chunks of space to load around my ship but as I move away from them they will be despawned and new chunks spawn in front. if you fly back you can see the older chunks just as they were earlier. just like a top down Minecraft. inside the chunks, I have code for randomly placed enemies and stars and whatnot but I have no idea how to spawn chunks. this should be an infinite world, no borders, you can fly as far as you want.
the chunk I have is just a black sprite 100/100
This is how they would load
as far as my skill goes I'm not new to coding but not great either so keep that in mind. I have no clue how to go about doing this so any help would be appreciated.
Thank You :)
Set the chunk dimension and compare your ship's position to it and you get the coordinate of the chunk that your ship is in. For example, if each chunk is 16x16 units big and your ship is at position 25,2 then in the chunk coordinates are 2,0 ($$anonymous$$athf.FloorToInt(pos / chunkSize). You could have references to your chunks in a 2-dimensional array where the indices correspond to the chunk coordinates you get from the ship position. This allows you to acces,create, or destroy chunks based on where you are and where you are going.
I'm not sure I understand what you mean. I don't really understand your example if it's possible for you to explain that a little more. And like how you would turn that into code. sorry. Thank you. :)
Your answer
Follow this Question
Related Questions
loading a scene seems to be slower 0 Answers
(c# source request) how to spawn a few bottom objects in a spiral to create a simple terrain? 1 Answer
What is the best way to load an instance of a gameObject when loading a saved game? 0 Answers
Generating Chunks 1 Answer
Prefab Seed-based Procedural Generation (Picture Inclu.) 1 Answer