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);
}
}