public void AddBlock(RaycastHit hitInfo, Vector3 direction, bool trueLocation, int blockType) { Vector3 hitInLocalSpace = transform.InverseTransformPoint(hitInfo.point - direction.normalized * .001f); int xCoord = (int)hitInLocalSpace.x; int yCoord = (int)hitInLocalSpace.y; int zCoord = (int)hitInLocalSpace.z; if (hitInLocalSpace.x % 1 > .5f) { xCoord++; } if (hitInLocalSpace.y % 1 > .5f) { yCoord++; } if (hitInLocalSpace.z % 1 > .5f) { zCoord++; } // Debug.Log("x: " + xCoord + ", Y: " + yCoord + "Z: " + zCoord); //check if added in next chunk. if (xCoord > 15 && !trueLocation) { rightChunk.AddBlock(hitInfo, direction, true, blockType); return; } if (hitInLocalSpace.x < 0 && !trueLocation) { leftChunk.AddBlock(hitInfo, direction, true, blockType); return; } if (zCoord > 15 && !trueLocation) { forwardChunk.AddBlock(hitInfo, direction, true, blockType); return; } if (hitInLocalSpace.z < 0 && !trueLocation) { backwardChunk.AddBlock(hitInfo, direction, true, blockType); return; } if (yCoord > 90) { return; } if (zCoord > 15) { zCoord = 15; } if (zCoord < 0) { zCoord = 0; } if (xCoord > 15) { xCoord = 15; } if (xCoord < 0) { xCoord = 0; } blockDataArr[xCoord, yCoord, zCoord] = new BlockDataInternal(); blockDataArr[xCoord, yCoord, zCoord].blockType = blockType; blockExistsArr[xCoord, yCoord, zCoord] = true; //Check to see if any block will be hidden: BlockDataInternal addedBlock = blockDataArr[xCoord, yCoord, zCoord]; addedBlock.visible = true; if (blockExistsArr[xCoord, yCoord - 1, zCoord]) //up { addedBlock.DownExists = true; blockDataArr[xCoord, yCoord - 1, zCoord].UpExists = true; } if (yCoord < 63 && blockExistsArr[xCoord, yCoord + 1, zCoord]) // down { addedBlock.UpExists = true; blockDataArr[xCoord, yCoord + 1, zCoord].DownExists = true; } if (xCoord > 0) { if (blockExistsArr[xCoord - 1, yCoord, zCoord]) // left { addedBlock.LeftExists = true; blockDataArr[xCoord - 1, yCoord, zCoord].RightExists = true; } } else { if (leftChunk.blockExistsArr[15, yCoord, zCoord]) { addedBlock.LeftExists = true; leftChunk.blockDataArr[15, yCoord, zCoord].RightExists = true; leftChunk.HideInvisible(); } } if (xCoord < 15) //right { if (blockExistsArr[xCoord + 1, yCoord, zCoord]) { addedBlock.RightExists = true; blockDataArr[xCoord + 1, yCoord, zCoord].LeftExists = true; } } else { if (rightChunk.blockExistsArr[0, yCoord, zCoord]) { addedBlock.RightExists = true; rightChunk.blockDataArr[0, yCoord, zCoord].LeftExists = true; rightChunk.HideInvisible(); } } if (zCoord > 0) { if (blockExistsArr[xCoord, yCoord, zCoord - 1]) // backward { addedBlock.BackExists = true; blockDataArr[xCoord, yCoord, zCoord - 1].ForwardExists = true; } } else { if (backwardChunk.blockExistsArr[xCoord, yCoord, 15]) { addedBlock.BackExists = true; backwardChunk.blockDataArr[xCoord, yCoord, 15].ForwardExists = true; backwardChunk.HideInvisible(); } } if (zCoord < 15) { if (blockExistsArr[xCoord, yCoord, zCoord + 1]) // forward { addedBlock.ForwardExists = true; blockDataArr[xCoord, yCoord, zCoord + 1].BackExists = true; } } else { if (forwardChunk.blockExistsArr[xCoord, yCoord, 0]) { addedBlock.ForwardExists = true; forwardChunk.blockDataArr[xCoord, yCoord, 0].BackExists = true; forwardChunk.HideInvisible(); } } HideInvisible(); }