# Placing walls, floors, furniture inGame in 3d

4

C#

### Question

Cheers, I would like to create simulator where the player can customize his house. So he shoukd be able to create walls, floors, and any kind of forniture. So the play select a wall for example from his inventory and can place it in the world, clipped to the ground and grid adjusted. Pretty much like in “The Sims”.
Whats the way to go? Where should I start?

Can’t you use preload maybe in combination with set_as_top_level() and set theyr Y position? something like:

func _process(_delta):
<
<if Input.is_action_pressed(“Spawn Furniture”):
<<
<<var furniture = table.instantiate()
<<furniture.position.y = …?
<<furniture.set_as_top_level(true)

So, generally, for aligning things to grids… Idk, there might be plugins that do it for you or something, but the math is not super complicated either.

For the sake of the example, let’s assume a 1-dimensional world (so we’re trying to place an object somewhere on a line).

So, say the user clicks at position `x` on our line, we want to take `x` and align it to our grid. For example, if our grid cells are 2 meters wide, and if `x` is 5.3, we want to change it to 6.

The way to do that is pretty simple. Divide `x` by the size of the grid cells, round that number to the nearest integer, and then multiply by the grid cell size.

``````const cell_size = 2

func align_to_grid(x):
return round(x/cell_size) * cell_size
``````

Then, you can do `x = align_to_grid(x)` before placing the object.

To work in 2D or 3D, you just have to align each coordinate individually using the same method.

Oh, and here is some documentation about raycasting from the screen, which is one way to figure out where the user is hovering their mouse. This page shows how to get a to- and from-point for a ray, and then you’d use ordinary raycasting (see further up on the same page) to find the objects that the ray is intersecting with. It’s also possible to use a collision mask to make the raycast ignore certain objects.