Before updating to Windows 10 you may need to return your Pro license. More information here

placing block on plane in a grid

i made a minecraft like game where i need to place block block on a plane i aready figurered out how to place block side to side but now what i whant to do is being able to ceperate a plane in a grid and place my block on the grid insted of in the center of the plane.how can i do this?

here is the block placing part of my script :

 var hit : RaycastHit;
 var range : float = 15.0f;
 var woodBlock:Transform;
 function Update () {
     if (Input.GetMouseButtonDown(0) && Hit()) {
         var cube : Transform = Instantiate(woodBlock);
         cube.transform.position = hit.transform.position + hit.normal;
     if (Input.GetMouseButtonDown(1) && Hit() &&hit.transform.gameObject.tag == "BuildMat"){
 function Hit() : boolean {
     return Physics.Raycast(transform.position, transform.forward, hit, range);
more ▼

asked Nov 05, 2011 at 04:59 PM

avatar image

16 28 20 24

If you want your chunks to center correctly on the chunk you clicked on to create the new one, than using plane as a floor may not be the best way to do it, i mean you do something like if(hit.transform.gameObject.tag == "Plane"){ round to nearest acceptable position } where hit.point is the world position of the place you clicked on the plane.. but it may be a bit easier to create a floor out of chunks.

Nov 06, 2011 at 12:18 PM chemicalvamp
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

I didn't understand much your question, but if each cell of your "grid" has the same size (as it definitely should be), then just calculate based on the click position.

Here's a very small example:

  • Let's say cell size is 10 by 10 units

  • Grid size is 20 cells wide by 20 cells long

  • Also, let's say the plane is located to that the (0,0,0) world coordinate is on the lower-left of the plane (just like when you create Terrains). This means the plane itself is not located at (0,0,0), so it's not "centered". This is not mandatory but if you change this, you have to add offsets to all calculations.

If the mouse click is at (138, 0, 179) -world position-, then you calculate like so:

cellSize = 10; mouseX = 138; mouseZ = 179; (we ignore mouse Y since we don't care)

columnClicked = Mathf.Floor(mouseX / cellSize); // in our example, the column clicked is 13 rowClicked = Mathf.Floor(mouseY / cellSize); // in our example, the row clicked is 17

So, at this point, you now the user clicked on the cell located in the coordinate (13, 17). To get the center of it, just add half the cellSize to the position coordinate. Meaning: cell 1 starts at X=0 and ends at X=10 horizontally; start at Z=0 and ends at Z=10 vertically cell 2 starts at X=10 and ends at X=20 and so forth... so, cell 13 starts at X=130 and ends at X=140

To get the center of the cell add half the size to it. So, the center of cell 13 would be at X=135.

Also, you should use Mathf.Floor or Mathf.Ceil depending if you want the cell coordinate to be zero-based.

I hope this helps and makes sense.

more ▼

answered Nov 06, 2011 at 04:42 PM

avatar image

91 4 4 4

thanx for your time :)

Nov 07, 2011 at 12:57 AM psychedelik
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Nov 05, 2011 at 04:59 PM

Seen: 3828 times

Last Updated: Nov 07, 2011 at 12:57 AM