In a FPS i'm trying to instantiate cube like the old good minecraft. The problem is that the first cube i instantiate on terrain got y=0 but half of that cube is underground ... All my cube prefab are 1x1x1.
How to fix this problem ?
void BuildBlock(GameObject block) { if (Physics.Raycast(shootingPoint.position, shootingPoint.forward, out RaycastHit hitInfo)) { if (hitInfo.transform.tag == "Block") { // Instance over other cube Vector3 spawnPosition = new Vector3(Mathf.RoundToInt(hitInfo.point.x + hitInfo.normal.x / 2), Mathf.RoundToInt(hitInfo.point.y + hitInfo.normal.y / 2), Mathf.RoundToInt(hitInfo.point.z + hitInfo.normal.z / 2)); Instantiate(block, spawnPosition, Quaternion.identity, parent); } else { // Instance on terrain Vector3 spawnPosition = new Vector3(Mathf.RoundToInt(hitInfo.point.x), Mathf.RoundToInt(hitInfo.point.y), Mathf.RoundToInt(hitInfo.point.z)); Instantiate(block, spawnPosition, Quaternion.identity, parent); } } }
Mathf.RoundToIntwhich snaps the position to a regular grid? Try to explain or illustrate what behaviour you want and we can match that. Presumably you already considered adding 0.5 to the y to lift the bottom of the cube up to the hit point. \$\endgroup\$