How the Modular Building System Works Behind the Scenes

Building Hierarchy
Buildings consist of a parent building object, everything else is attached to this (such as walls, pillars etc). As players add more modules to the building its parent is automatically set to the building object. This is the origin point of the building and everything else is placed in relation to this via it's local space. In this way different buildings can have unique orientations although it's fully possible with a little work to use a single grid if you wanted all buildings to have the same orientation or be aligned to north, east, south, west etc.

Item Structure
Items consist of 3d Meshes, Prefabs and some Scripts. When equipping an item essentially what the system does is instantiate the desired Item just like you would any other prefab. Once the prefab gets instantiated it's scripts initialize and then start running. When items are unequipped they are destroyed causing them to stop running.

Each item prefab has a script, this script tells each module how to behave under different circumstances. For instance, when placing foundations, are we placing the foundation on the terrain? or attaching it to another foundation? This is also where we do checks such as checking if a wall has a pillar either side before allowing the player to place it.

Meshes
Each Building Module has a Placement Mesh and also a World Mesh.

The Placement Mesh acts as a placement indicator changing between red and green indicating whether the module can or cannot be placed and it also acts as a preview. This mesh uses a semi transparent material.

The World Mesh is the mesh that actually exists in the world once a player has placed a building module. It uses different textures compared to the placement mesh and the player can also collide with this layer.

Item Prefab
This is a prefab with an item script attached to it, this script is where we can set the Placement Mesh and World Mesh, the size of the grid which this building module snaps to and which layers the ray caster interacts with. The item prefab brings together the meshes and scripts to create equippable items which can be created and destroyed at run time. All having unique behaviors where required.