- Home /
Instantiating cubes at runtime, align them
I'm making a little game demo where I'm playing around with some ideas. At the moment I'm allowing my player to build his own house in a way very similar to Minecraft. A cube prefab serves as the building block and the user can use his mouse to instantiate a cube wherever the mouse is pointing.
This works at the moment but it is very hard to get the cubes aligned properly, to make a "smooth" house.
Does anyone have any clues to help me make my blocks "snatch" together (ie: Minecraft building) instead of having them be instantiated exactly where the mouse cursor is pointing.
@chrisall76's solution will assure that all the blocks are aligned along a grid. There is a more complicated approach that uses raycasting to attach a cube to the side of cube clicked on. What do you need?
Answer by chrisall76 · Nov 30, 2013 at 09:05 PM
When instantiating, round the numbers. Untested but it should work.
Vector3 objectPos = Camera.current.ScreenToWorldPoint((int)mousePos);
Instantiate(yourObject, objectPos, Quaternion.identity);
Your answer
![](https://koobas.hobune.stream/wayback/20220613123126im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Instantiating prefabs, when there is nothing 2 Answers
Adding Imperfections? 1 Answer
Checking if object intersects? 1 Answer
Placing static prefabs on click 2 Answers