我正在開發類似Minecraft的遊戲,並嘗試讓塊刪除正常工作。我有一個函數返回塊的正確座標,然後調用DestroyBlock(下面),它在立方體的每個方向上投射6條射線,並且應該銷燬它遇到的任何四邊形。然後我有另一個函數,在塊被銷燬之後調用,然後更新周圍的塊並呈現適當的面。我的問題是,這個DestroyBlock函數並不總是工作。正如你在第一次點擊的圖像中看到的那樣,一切正常工作,並且正確的面孔被破壞然後被渲染,但是當我做第二次點擊時,它只會破壞新塊的一面... Unity3D Raycast不會每次都摧毀遊戲對象
void DestroyBlocks(int x, int y, int z) {
Vector3 origin = new Vector3(x, y, z);
Ray Up = new Ray(origin + Vector3.up, Vector3.down);
Ray Down = new Ray(origin + Vector3.down, Vector3.up);
Ray North = new Ray(origin + Vector3.forward, Vector3.back);
Ray East = new Ray(origin+ Vector3.right, Vector3.left);
Ray South = new Ray(origin + Vector3.back, Vector3.forward);
Ray West = new Ray(origin + Vector3.left, Vector3.right);
RaycastHit markedQuad;
if(Physics.Raycast(Up, out markedQuad, 1f)) {
Destroy(markedQuad.transform.gameObject);
Debug.Log("Destroyed Up");
}
if(Physics.Raycast(Down, out markedQuad, 1f)) {
Destroy(markedQuad.transform.gameObject);
Debug.Log("Destroyed Down");
}
if(Physics.Raycast(North, out markedQuad, 1f)) {
Destroy(markedQuad.transform.gameObject);
Debug.Log("Destroyed North");
}
if(Physics.Raycast(East, out markedQuad, 1f)) {
Destroy(markedQuad.transform.gameObject);
Debug.Log("Destroyed East");
}
if(Physics.Raycast(South, out markedQuad, 1f)) {
Destroy(markedQuad.transform.gameObject);
Debug.Log("Destroyed South");
}
if(Physics.Raycast(West, out markedQuad, 1f)) {
Destroy(markedQuad.transform.gameObject);
Debug.Log("Destroyed West");
}
}
而且我知道,光線投射更是創下因爲調試信息的四邊形,但由於某種原因,它實際上沒有摧毀所有的邊,當它擊中他們。任何幫助或建議將不勝感激。
四邊形的邊緣有多長? 1個單位?要檢查光線是否碰到正確的四邊形,可以在刪除它們之前重命名相應的gameObjects。例如,使用「Destroy」的時間參數在30秒後刪除它們。通過這種方式,您可以輕鬆識別檢查員中的對象。 –
感謝您的建議! – Denny