City Builder Construction System
One of my dreams to create a strategy and/or city builder game. I often work towards this goal in small bursts, learning how to create the systems necessary to make such a game work over time. The most common system I experiment with is the actual placement of the buildings themselves, which involves raycasting to a position in the game space.
My most recent attempt at creating a building placement system was inspired by the game Majesty: The Fantasy Kingdom Sim. In Majesty new buildings are placed with a ring of detection. If another building is within that ring of detection the new building cannot be built and must be placed elsewhere
Below is an example of this:
Below is a video of my system. I think it pretty accurate to the one that is used in Majesty. Each building also uses a Scriptable object o hold specific information about each building so that designers/collaborators could easily make new buildings or change existing ones.
The placement system in the video works as follows:
The system uses a raycast to find a valid location which requires a collision with an object on the Ground layer of the game. Once the raycast collides with a valid target, that collision point is taken and a projection of the new building is instantiated there. The building will move around as/if the raycast changes position.
When the new building starts being projected a ring is also projected visually, however, in actuality a sphere collider is being created. The sphere collider’s radius is equal to the ring’s radius and thus captures the same area. The sphere collider detects for collisions of other buildings (which should be on a layer called Buildings).
If the sphere collider does not detect any other buildings in its area then the building can be placed. The building is then deselected and the projected building becomes invisible.
If the collider detects another building in its area then the building is not placed and the player is given another opportunity to place the building.
The current scriptable object I have has a couple of parameters that makes instantiating and tweaking the buildings easier for the placement system.
Obvious fields are name, and height offset (so the building does instantiate partway into the ground), but the Placement Col variables are what set the detection ring’s size while the Col variables set the actual building’s collider.
A few more additional variables I added to the Scriptable objects is the ability to add a mesh, which allows for quick aesthetic changes to the buildings and a Material that should allow the buildings to look different once built if the I so choose.