你好,我正在嘗試創建一種MMO相機風格,玩家可以拖動鏡頭觀察角色周圍,當他向前移動時,鏡頭慢慢旋轉到玩家身后,當鏡頭復位時,它選擇在玩家的左側或右側移動,哪一個是最短路徑。目前,當你需要將360度旋轉到0度時,下面的代碼無法作業。
[作業示例][1]
[作業實體][2]
[不作業的例子][3]
[1]: https:/i.stack.imgur.com/LblS0.png
[2]: https://i.stack.imgur.com/ujiSs.png
[3]: https://i.stack.imgur.com/zpHGE.png
float yMaxRotation; //is our target rotation (our Green Pointer)
float yRotation; //是我們的攝像機旋轉(我們的灰色指標)。
yMaxRotation = target.rotation.Z - (MathHelper.PiOver2);
yMaxRotation = yMaxRotation % MathHelper.ToRadians(360)。
if (yMaxRotation < 0) yMaxRotation = MathHelper.ToRadians(360) 。
float newRotation = yMaxRotation;
if (yMaxRotation <= MathHelper.ToRadians(90) && yRotation >= MathHelper.ToRadians(270) )
{
newRotation = yMaxRotation - MathHelper.ToRadians(360)。
}
if (yMaxRotation >= MathHelper.ToRadians(270) & & yRotation <= MathHelper.ToRadians(90)
{
newRotation = yMaxRotation MathHelper.ToRadians(360)。
}
if (yRotation <= newRotation)
{
yRotation = (newRotation - yRotation) / 15;
}
if (yRotation > newRotation)
{
yRotation -= Math.Abs(newRotation - yRotation) / 15;
}
而對于處理距離和方向的攝像機來說,每次更新都會呼叫這段代碼
Position //是我們相機的位置。
newPos //是我們玩家的角色位置--偏移量(將攝像機向后和向上推)。
Vector3 newPos = ((target.position - target.positionOffset) new Vector3(0, 0, 18)。
SetLookAt(Position, newPos, Vector3.Backward)。
uj5u.com熱心網友回復:
在比較角度的時候,數學并沒有把1和359之間的距離等同于2。
float yMaxRotation; //is our target rotation (our Green Pointer)
float yRotation; //是我們的攝像機旋轉(我們的灰色指標)。
yMaxRotation = target.rotation.Z - MathHelper.PiOver2; //copy from source.
float newRotationDelta = (yMaxRotation - yRotation - MathHelper.Pi -
MathHelper.TwoPi)% MathHelper.TwoPi MathHelper.Pi; //比例-179到180。
yRotation = newRotationDelta / 15。
yMaxRotation - yRotation得到delta角度。減去540(180 360或360的任何倍數,因為mod會移除它,以確保該值是負的)以創建一個負的x軸反射角,moded到范圍(-359到0),然后加回180以扭轉反射并重新定位于0。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/331093.html
標籤:
