void Update() { for (var i = 0; i < keyCodes.Length; i++) { if (Input.GetKeyDown(keyCodes[i]) && i < buildableBlocks.Length) { blockTypeToBuild = buildableBlocks[i]; blockTypeSelected(); } } if (Input.GetMouseButtonDown(1)) { if (!interactor.isBlockSelected) { return; } // Prevent building on blocks where player stays var playerPosition = Vector3i.FloorToInt(playerTransform.localPosition - new Vector3(0, 0.5f, 0)); var upperPosition = playerPosition + new Vector3i(0, 1, 0); var placeblePosition = interactor.placeblePosition + interactor.selectedChunk.chunkPosition; if (playerPosition != placeblePosition && upperPosition != placeblePosition) { interactor.selectedChunk.CreateBlock(blockTypeToBuild, interactor.placeblePosition); } } }
bool SelectBlock() { var hitPosition = hit.collider.transform.position; var chunkPosition = World.GetChunkPosition(hitPosition); selectedChunk = world.GetChunk(chunkPosition); if (!selectedChunk) { return(false); } var outsideHit = Vector3i.FloorToInt(hit.point + hit.normal / 2); var insideHit = Vector3i.FloorToInt(hit.point - hit.normal / 2); placeblePosition = outsideHit - chunkPosition; var insidePosition = insideHit - chunkPosition; if (selectedPosition != insidePosition) { selectedPosition = insidePosition; selectedBlockChanged(); } return(true); }
public static Vector3i GetChunkPosition(Vector3 position) { return(Vector3i.FloorToInt(position / Chunk.size) * Chunk.size); }