DAY 13
0
Mobile Development

魔術方塊的偵測

魔術方塊狀態

`````` public List<GameObject> front = new List<GameObject>();
``````

射線設置

• 為各射線都宣告一個變數儲存射線位置 `public Transform tUp;`
• 魔術方塊的面為3x3，因此射線也宣告成3x3的形式，宣告rayCount做為控制迴圈變數
• 並利用迴圈生成射線，生成射線順序從左上到右下:
|0|1|2|
|3|4|5|
|6|7|8|
• 為射線命名完後，設定旋轉數值，並回傳射線
``````public GameObject empty; //射線起始的位置
List<GameObject> BuildRays(Transform rayTransform, Vector3 direction)
{
int rayCount = 0;
List<GameObject> rays = new List<GameObject>();

for (int y = 1; y > -2; y--)
{
for (int x = -1; x < 2; x++)
{
Vector3 startPos = new Vector3( rayTransform.localPosition.x + x,
rayTransform.localPosition.y + y,
rayTransform.localPosition.z);
GameObject rayStart = Instantiate(empty, startPos, Quaternion.identity, rayTransform);
rayStart.name = rayCount.ToString(); //射線編號
rayCount++;
}
}
rayTransform.localRotation = Quaternion.Euler(direction);
return rays;
}
``````

`````` void SetRayTransforms()
{
upRays = BuildRays(tUp, new Vector3(90, 90, 0));
downRays = BuildRays(tDown, new Vector3(270, 90, 0));
leftRays = BuildRays(tLeft, new Vector3(0, 180, 0));
rightRays = BuildRays(tRight, new Vector3(0, 0, 0));
frontRays = BuildRays(tFront, new Vector3(0, 90, 0));
backRays = BuildRays(tBack, new Vector3(0, 270, 0));
}
``````

讀取面狀態

Raycast運用

`````` private int layerMask = 1 << 8;
``````

• 新增faceHit儲存射線碰撞的所有面
• 建立Raycast，並判斷是否與魔術方塊面相交
• 使用DrayRay，將射線畫出來,方便後續除錯
`````` public List<GameObject> ReadFace(List<GameObject> rayStarts, Transform rayTransform)
{
List<GameObject> facesHit = new List<GameObject>();

foreach (GameObject rayStart in rayStarts)
{
Vector3 ray = rayStart.transform.position;
RaycastHit hit;

if (Physics.Raycast(ray, rayTransform.forward, out hit, Mathf.Infinity, layerMask))
{
Debug.DrawRay(ray, rayTransform.forward * hit.distance, Color.yellow);
}
else
{
Debug.DrawRay(ray, rayTransform.forward * 1000, Color.green);
}
}
return facesHit;
}
``````

讀取面的狀況

``````    public void ReadState()
{
cubeState = FindObjectOfType<CubeState>();

cubeState.up = ReadFace(upRays, tUp);
cubeState.down = ReadFace(downRays, tDown);
cubeState.left = ReadFace(leftRays, tLeft);
cubeState.right = ReadFace(rightRays, tRight);
cubeState.front = ReadFace(frontRays, tFront);
cubeState.back = ReadFace(backRays, tBack);
}
``````