How do I create a grid system so when I click and spawn objects they snap into place?

Hi, I’m new to unity, not too new but my knowledge is average and I have decent amount of knowledge.
To start off thank you for reading my thread if I’ve posted in the wrong section, sorry in advanced.
I’ve developed a first person camera control with an empty object as my character.
I’m developing a map editor 3D and using a terrain, I can click and and spawn objects in run time based on my cursor (point and click), this is attached to my camera, and my camera is attached to my player object.
I can spawn prefab by left clicking my mouse, it shows the objects I’ve spawned. I’ve disabled rigidbody on my code and trigger is on when I hold left click my object spawns but once I let go the object drops and rigid body is activated and trigger is disabled.
I need a grid system so the objects accurately spawn on top of each other and don’t fall off if not placed correctly on top of each other.
Sorry for any grammatical and spelling mistakes, thanks.

make the position Mathf.Round() to nearest 1 … or whatever #

(if you want objects in any position to snap to another object)
what you can do is put emptys as children of objects, they represent snapping positions,
then you get the parent object of this from the click, and whichever empty you want, or nearest to click , you Instantiate the other prefab to that empty … the empty acts as the origin position

1 Like