2D grid for building

Hello,

I try to make a 2D building game. The part of the code is supposed to Instantiate a cube wherever the player clicks. But it must be aligned as a grid (2D minecraft).

The problem is that the “Mathf.FloorToInt” I use converts for example the float “0,2” → “0”, so the Cube is instantiated one unit to the left, not on the part of the grid the user clicks.

Any help?

#pragma strict
var block : GameObject;

function Update () {
var pos : Vector3 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
var posIntX : int =  Mathf.FloorToInt(pos.x);
var posIntY : int = Mathf.FloorToInt(pos.y);
if(Input.GetMouseButtonDown(0))
{
Instantiate(block,Vector3(posIntX,posIntY,0),transform.rotation);
}
}

var Block:GameObject;
var squareSize : float = 1.0f;

function Update () {
if (Input.GetMouseButtonDown(0))
{
var mousePos : Vector3 = Input.mousePosition;
mousePos.z = 10;
var objectPos : Vector3 = Camera.main.ScreenToWorldPoint(mousePos);
objectPos.x = Mathf.Round(objectPos.x/squareSize) * squareSize;
objectPos.y = Mathf.Round(objectPos.y/squareSize) * squareSize;
objectPos.z = Mathf.Round(objectPos.z/squareSize) * squareSize;
Instantiate(Block, objectPos, Quaternion.identity);
}
}