using UnityEngine;
public class Block : MonoBehaviour {
// 定義方塊的材質和形狀
public Material material;
public Vector3Int shape = Vector3Int.one;
// 堆疊方塊
public void Stack(Vector3Int position) {
GameObject blockObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
blockObject.transform.position = position;
blockObject.GetComponent().material = material;
blockObject.GetComponent().isTrigger = false;
blockObject.AddComponent().material = material;
blockObject.AddComponent().shape = shape;
}
// 拆除方塊
public void Remove() {
Vector3Int position = Vector3Int.RoundToInt(transform.position);
for (int x = 0; x < shape.x; x++) {
for (int y = 0; y < shape.y; y++) {
for (int z = 0; z < shape.z; z++) {
Vector3Int offset = new Vector3Int(x, y, z);
Vector3Int targetPosition = position + offset;
Collider[] colliders = Physics.OverlapBox(targetPosition + new Vector3(0.5f, 0.5f, 0.5f), new Vector3(0.5f, 0.5f, 0.5f));
foreach (Collider collider in colliders) {
if (collider.gameObject.GetComponent()) {
Destroy(collider.gameObject);
}
}
}
}
}
}
}