In this blogpost I'll be going over a personal project. I took a rather unconventional route to make a city generator. But the end result is a very good visual representation how city generators work.
The Plan
The plan was to generate a 2D grid using a 2D array. Then selecting certain points on that grid as starting points for the roads. The roads starting points will then check the tiles next to it and decide if it can spawn a road there. When the road building phase is done, all the road tiles will again check all the tiles surrounding it. If there is not a road, then place a house.
Sounds easy right?
![]() |
2D Grid (of green tiles)
|
The Execution
I started with the 2D array and placing the road starting points. This all went pretty well. When it was time to build the rules to make curves and intersections, I kinda ran out of knowledge and asked for help. Liam Ederzeel was the one to help me for 3 hours straight. We got a lot done but it was all above my skill level. So it was time for a plan B. I kept his code for reference because I did learn a lot from that. But I went back to the drawing table and thought how I would tackle a problem like this.
![]() |
Walker bots working hard |
Plan B
So I came up with the following method: Spawn a 2D grid using a 2D array. Spawn walker "bots" which will move in one direction an place roads. Attach a script to each tile to look around, if there is nothing (or grass) next to it, place a House. After some tinkering I got this to work.
Click here to see the finished product (Unity Webplayer is required).
WASD to move around
Mouse to look
Space to spawn bots
Enter/Return to spawn houses (ONLY DO THIS WHEN BOTS HAVE DISAPPEARED)
WASD to move around
Mouse to look
Space to spawn bots
Enter/Return to spawn houses (ONLY DO THIS WHEN BOTS HAVE DISAPPEARED)
![]() |
The Generated City |
Geen opmerkingen:
Een reactie posten