# Snapping Walls Together

Hello, I'm brand new to Unity and I am trying to create a construction type game.

What I want is to be able to create walls in game and be able to place them wherever according to a grid system. What really gets me is how to make it so that the walls snap together well to create fluid corners (90* is fine) or "T" or "+" intersections.

I understand that this type of snapping together requires a grid based game.

I would Also like to be able to create walls this way in game as well as in the editor, is that possible?

I'm no expert at scripting, so I could really use the help, Thanks a million

Without an understanding of a scripting language you are going to have a difficult creating a form of procedural or user created content system. This type of system is going to heavily integrate into how you store and interact with your map through out the game.

That being said, I would recommend going with a simple tile approach for generating and managing the map. I’ve implemented this in a couple ways and it has worked well for me.

Sharing an entire script implementation here would be tough, but I can cover the concepts.

Define the size of your game space grid (ie. 30 tiles by 30 tiles) Then you box this area in with a static object (or dynamically created if your size changes). Easiest being to start your map at 0,0 and expand positive in the X and Z directions.

Once you have that you need to define what your Tile Scale is. Tile Scale would refer to the size of a 3D object that takes up 1 tile. For example if your tiles are 3.0 by 3.0 units that would be a 3 tile scale. If they are 1 by 1 then it’s 1. Your 3D models should fit that tile scale, ie. a wall is Tile Scale units by Tile Scale units.

Having this value you can easily convert from World Space (3d X/Z) to Tile Space (2d X/Y). World Space to Tile Space is dividing the X/Z by the Tile Scale; reverse is multiplication. When a user interacts on a space and you perform a Raycast you receive back the 3D point, you translate to Tile Space, and work with your data, and can easily go back and forth from Tile Space to World Space.

Most tile systems would store their tiles in a 1 or 2 dimensional array, and keep it synchronized with the 3D game objects. Others might create the array at runtime for procedural generation, or others might have an array to start with and build the 3D world and then drop the array. It all depends on what data you need to store in the tile, and if the gameObject can store that data instead.

Wish I could provide more implementation details, but the scale/scope of it seems quite large.

Under edit, in Unity, there is a setting called snap settings, that will work in the editor, not in game.

How about getting this kind of snapping to occur during runtime?

Perhaps you could try creating a “Tile” system. It works well with the grid system you want to use. You would instansiate gameObjects according to an array, which would keep the x,y,z positon/rotation/scale and the object in the “tile”.

Also,
If each wall has its own collider then as the walls connect to each other the colliders must not touch or you have a constant collision and the wall colliders will not respond correctly to other impacts.