public void GetArroundBlock(BlockV2 block, Vector3 blockPos) { block.x_add = RayCastSide(blockPos, normalX); if (block.x_add != null) { block.x_add.x_sub = block; } block.x_sub = RayCastSide(blockPos, -normalX); if (block.x_sub != null) { block.x_sub.x_add = block; } block.y_add = RayCastSide(blockPos, normalY); if (block.y_add != null) { block.y_add.y_sub = block; } block.y_sub = RayCastSide(blockPos, -normalY); if (block.y_sub != null) { block.y_sub.y_add = block; } block.z_add = RayCastSide(blockPos, normalZ); if (block.z_add != null) { block.z_add.z_sub = block; } block.z_sub = RayCastSide(blockPos, -normalZ); if (block.z_sub != null) { block.z_sub.z_add = block; } }
private void GetPos() { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; Physics.Raycast(ray, out hit); if (hit.collider == null) { return; } BlockWall wall = hit.collider.GetComponent <BlockWall>(); if (wall != null) { Quaternion trans = wall.area.RedoQuaternoin; Vector3 localPos = trans * (hit.point - wall.area.transform.position); int targetX = (int)(localPos.x / wall.area.BlockSize); int targetY = (int)(localPos.y / wall.area.BlockSize); int targetZ = (int)(localPos.z / wall.area.BlockSize); wall.area.PlaceItem(prefab, targetX, targetY, targetZ); } BlockV2 block = hit.collider.GetComponent <BlockV2>(); if (block != null) { Vector3 dir = hit.point - block.transform.position; dir = block.area.RedoQuaternoin * dir; BoxSide side = GetSite(dir); int targetX = block.x; int targetY = block.y; int targetZ = block.z; switch (side) { case BoxSide.x_add: targetX++; break; case BoxSide.x_sub: targetX--; break; case BoxSide.y_add: targetY++; break; case BoxSide.y_sub: targetY--; break; case BoxSide.z_add: targetZ++; break; case BoxSide.z_sub: targetZ--; break; } block.area.PlaceItem(prefab, targetX, targetY, targetZ); } }
public BlockV2 RayCastSide(Vector3 pos, Vector3 dir) { result = new RaycastHit[1]; Physics.RaycastNonAlloc(pos, dir, result, blockSize); if (result[0].transform == null) { return(null); } Debug.DrawLine(pos, result[0].point); BlockV2 block = result[0].transform.GetComponent <BlockV2>(); return(block); }
public void PlaceItem(GameObject prefab, int xNum, int yNum, int zNum) { if (xNum < 0 || xNum >= x || yNum < 0 || yNum >= y || zNum < 0 || zNum >= z) { return; } GameObject obj = Instantiate(prefab); obj.gameObject.SetActive(true); obj.transform.parent = blockParent; obj.transform.localPosition = new Vector3((xNum + 0.5f) * BlockSize, (yNum + 0.5f) * BlockSize, (zNum + 0.5f) * BlockSize); obj.transform.localEulerAngles = Vector3.zero; BlockV2 block = obj.GetComponent <BlockV2>(); block.SetXYZ(xNum, yNum, zNum); block.area = this; GetArroundBlock(block, block.transform.position); }