- Home /
2d minecraft survival game
I am trying to make a 2d minecraft like survival game as a hobby. I am having a problem getting a grid and level generation to work. I have block selection, A flat level generation, a way to place blocks but not break them, and that's all I have so far.
If you see the blocks are not placed in a grid instead they are everywhere:
Do you guys know how I can get a grid going and how I can get it to delete blocks. So far blocks are placed at my mouse position using a raycast. All help is appreciated!
If you show your placement code, someone is likely will help you figure out out to align the blocks. It would help to know the world size of your blocks. We ask that each question only have one issue, so I'd post breaking blocks as a separate issue. Note that for large-world, $$anonymous$$ecraft-type games, the 'blocks' are not handled as individual game objects. Search UA for '$$anonymous$$ecraft' or 'voxel' for more information.
Answer by Pecek · Mar 08, 2014 at 11:44 AM
You can create a "grid" by rounding the value where you place your block. For instance:
if Physics.Raycast(ray, hit):
go.transform.position = hit.point
go.transform.position = Vector3(Mathf.Round(go.transform.position.x),Mathf.Round(go.transform.position.y),Mathf.Round(go.transform.position.z))
ps: yep, I'm using boo, don't judge me.
you could use this too:
int Heightgrid = 32;
int Widthgrid = 32;
gameObject.position = new Vector3($$anonymous$$athf.Round($$anonymous$$athf.Floor(gameObj.position.x / Widthgrid)* Widthgrid + Widthgrid/2.0f),
$$anonymous$$athf.Round($$anonymous$$athf.Floor(gameObj.position.y / Heightgrid)* Heightgrid + Heightgrid/2.0f), gameObject.position.z);
With this code the objects will be aligned on a grid (I hope).
Thank you, I am wondering how I would delete a block though. Everything is placed in a grid now but the sands physics fall out of the grid though because its a free block. So does anyone know how I could do sand and make the ability to break blocks
Your comment helped me and I got deleting to work but now I am wondering how to not place blocks on top of each other. I am also wondering how to make a world generate with mountains and things. Also jumping, I tried checking for ground but he has infinite jump.
Your answer
![](https://koobas.hobune.stream/wayback/20220613135701im_/https://answers.unity.com/themes/thub/images/avi.jpg)