// Update is called once per frame void Update() { bool leftClick = Input.GetMouseButtonDown(0); bool rightClick = Input.GetMouseButtonDown(1); if (leftClick || rightClick) { RaycastHit hitInfo; if (Physics.Raycast(transform.position, transform.forward, out hitInfo, maxDist, groundLayer)) { Vector3 pointInTargetBlock; //destroy if (rightClick) { pointInTargetBlock = hitInfo.point + transform.forward * .01f;//move a little inside the block } else { pointInTargetBlock = hitInfo.point - transform.forward * .01f; } //get the terrain chunk (can't just use collider) int chunkPosX = Mathf.FloorToInt(pointInTargetBlock.x / 16f) * 16; int chunkPosZ = Mathf.FloorToInt(pointInTargetBlock.z / 16f) * 16; ChunkPos cp = new ChunkPos(chunkPosX, chunkPosZ); TerrainChunk tc = TerrainGenerator.chunks[cp]; //index of the target block int bix = Mathf.FloorToInt(pointInTargetBlock.x) - chunkPosX + 1; int biy = Mathf.FloorToInt(pointInTargetBlock.y); int biz = Mathf.FloorToInt(pointInTargetBlock.z) - chunkPosZ + 1; if (rightClick)//replace block with air { inv.AddToInventory(tc.blocks[bix, biy, biz]); tc.blocks[bix, biy, biz] = BlockType.Air; tc.BuildMesh(); } else if (leftClick) { if (inv.CanPlaceCur()) { tc.blocks[bix, biy, biz] = inv.GetCurBlock(); tc.BuildMesh(); inv.ReduceCur(); } } } } }
private void Update() { Ray ray = new Ray(transform.position, transform.forward); hasSth = Physics.Raycast(ray, maxDistance, modifyLayer); bool leftClick = Input.GetMouseButtonDown(0); if (leftClick) { RaycastHit hitInfo; if (Physics.Raycast(transform.position, transform.forward, out hitInfo, maxDistance, modifyLayer)) { Vector3 pointInTargetBlock = hitInfo.point + transform.forward * .01f; //move a little inside the block int cubePosX = Mathf.FloorToInt(pointInTargetBlock.x); int cubePosY = Mathf.FloorToInt(pointInTargetBlock.y); int cubePosZ = Mathf.FloorToInt(pointInTargetBlock.z); Debug.LogFormat("cubePosX:{0},PosY:{1},PosZ:{2}", cubePosX, cubePosY, cubePosZ); int chunkPosX = Mathf.FloorToInt(pointInTargetBlock.x / TerrainChunk.chunkWidth) * TerrainChunk.chunkWidth; int chunkPosZ = Mathf.FloorToInt(pointInTargetBlock.z / TerrainChunk.chunkWidth) * TerrainChunk.chunkWidth; Debug.LogFormat("ChunkPosX:{0},ChunkPosZ:{1}", cubePosX, cubePosZ); TerrainChunk chunk = TerrainGenerator.chunks[new ChunkPos(chunkPosX, chunkPosZ)]; chunk.blocks[cubePosX - chunkPosX, cubePosY, cubePosZ - chunkPosZ] = BlockType.Air; chunk.BuildMesh(); } } }
void Update() { bool place = controls.Player.Place.ReadValue <float>() == 1; bool mine = controls.Player.Mine.ReadValue <float>() == 1; bool clicking = mine || place; if (clicking && (DateTimeOffset.Now.ToUnixTimeMilliseconds() - last >= DELAY || !used)) { var c = PlayerLook.GetRotation(); Vector3 dir = c * Vector3.forward; if (Physics.Raycast(Camera.main.transform.position, dir, out RaycastHit hit, interactDist, groundLayer)) { used = true; if (place) { dir *= -1; } Vector3 target = hit.point + dir * .01f; if (place && CannotPlace(target, player.position)) { return; } ChunkPos pos = terrainGenerator.GetChunkPosition(target); TerrainChunk tc = terrainGenerator.GetChunkAt(pos); int bix = Mathf.FloorToInt(target.x) - pos.x + 1; int biy = Mathf.FloorToInt(target.y); int biz = Mathf.FloorToInt(target.z) - pos.z + 1; if (mine) { if (CanBreak(tc.Blocks[bix, biy, biz])) { tc.Blocks[bix, biy, biz] = BlockType.Air; } } else { tc.Blocks[bix, biy, biz] = inventory.Current; } last = DateTimeOffset.Now.ToUnixTimeMilliseconds(); tc.BuildMesh(); } } if (!clicking) { used = false; } }
public void BuildChunk(int xPos, int yPos, int zPos) { GameObject chunkGO = Instantiate(terrainChunkPrefab, new Vector3(xPos, yPos, zPos), Quaternion.identity); TerrainChunk chunk = chunkGO.GetComponent <TerrainChunk>(); for (int x = 0; x < TerrainChunk.chunkWidth; x++) { for (int z = 0; z < TerrainChunk.chunkWidth; z++) { for (int y = 0; y < TerrainChunk.chunkHeight; y++) { chunk.blocks[x, y, z] = GetBlockType(xPos + x, yPos + y, zPos + z); } } } chunk.BuildMesh(); chunks.Add(new ChunkPos(xPos, zPos), chunk); debugList.Add(new Vector3(xPos, 0, zPos)); }