DAY 21
0
Mobile Development

## 變數宣告

``````static class Dir
{
public const int Top = 0;
public const int Right = 1;
public const int Down = 2;
public const int Left = 3;
public const int no = -1;
}
``````

``````int inputDir = Dir.no;
``````

``````int Direction = Dir.Right;
``````

``````Vector3 movement;
``````

``````bool rotating = false;
``````

``````bool turntoright = false;
``````

``````float rotateangle = 0;
``````

``````float oriAngle = 90;
``````

## 前進

``````void FixedUpdate(){
if(inputDir != Dir.no) {
rb.MovePosition(rb.position + movement * movespeed * Time.fixedDeltaTime);
}
}
``````

## 旋轉

• 先判斷是否需要旋轉(以rotating作為flag)
• 開始累積rotateangle
• 用rotateangle判斷有沒有轉超過90度了
• 如果還沒超過，就再繼續判斷式順時針還是逆時針，並使用localRotation做旋轉
• 如果超過了，就判斷旋轉方向，旋轉到由原本的角度再旋轉90度，並停止旋轉，歸零選轉角度
``````if (rotating){//判斷有沒有旋轉
rotateangle += rotatespeed * Time.deltaTime;

if (rotateangle < 90f){ //還沒轉玩90度
if (turntoright)
{
transform.localRotation = Quaternion.Euler(0, transform.localEulerAngles.y + rotatespeed * Time.deltaTime, transform.localEulerAngles.z);
}
else
{
transform.localRotation = Quaternion.Euler(0, transform.localEulerAngles.y - rotatespeed * Time.deltaTime, transform.localEulerAngles.z);
}
}
else
{
if (turntoright)
{
transform.localRotation = Quaternion.Euler(0, oriAngle - 90f, transform.localEulerAngles.z);
oriAngle = oriAngle + 90f;
}
else
{
transform.localRotation = Quaternion.Euler(0, oriAngle + 90f, transform.localEulerAngles.z);
oriAngle = oriAngle - 90f;
}

rotating = false;
rotateangle = 0;
}
}
``````