我正在嘗試制作一個飛機控制器,我的目標是在街機和現實之間,所以我希望飛機以與滾轉成正比的力轉動。
我沒有進行任何調整,我仍在對整個事情進行原型設計,但是我在使用四元數時遇到了獲得帶符號旋轉角的問題,我在 SO 上查看了確定四元數旋轉是順時針還是逆時針但是我無法將解決方案推廣到旋轉可以位于的(幾乎)任意平面。
我現在做的:
private void FixedUpdate()
{
float desiredYaw = _yaw * _rotationSpeed * Time.fixedDeltaTime;
float desiredPitch = -_pitch * _rotationSpeed * Time.fixedDeltaTime;
float rotationStepSize = _throttle * Time.fixedDeltaTime;
Quaternion toRotate = Quaternion.Euler(desiredPitch, 0, desiredYaw);
Quaternion straighRotation = Quaternion.LookRotation(_transform.forward, Vector3.up );
_rotation = _transform.rotation * toRotate;
float turningForce = Quaternion.Angle( _rotation, straighRotation );
_rigidbody.MoveRotation( _rotation );
_rigidbody.AddTorque( turningForce * _rotationForce * rotationStepSize * Vector3.up );
_rigidbody.AddRelativeForce( _speed * rotationStepSize * Vector3.forward );
}
編輯:我意識到我正在使用滾動而不是偏航來計算轉動力,這只是錯誤的措辭,現在更正了。
uj5u.com熱心網友回復:
由于您所需要的只是一個描述飛機右側向下程度的因素,因此您只需使用飛機右側的 y 分量即可。無需引入四元數甚至三角函式。評論中的解釋:
private void FixedUpdate()
{
// ...
// Calculate how downward local right is in range [-1,1]
// The more downward, the more tilted right the plane is
// positive = tilted right
// negative = tilted left
float turnFactor = -_transform.right.y;
// Could do things to modify turnFactor to affect easing here.
// For instance, if turning rate should start slower then rapidly increase:
// turnFactor = Mathf.Sign(turnFactor) * turnFactor * turnFactor;
// Use factor and _rotationForce member to calculate torque, apply along
// global up.
// We expect to call this every fixed frame so we can just use the default
// ForceMode of ForceMode.Force which multiplies fixed delta time inside.
_rigidbody.AddTorque(_rotationForce * turnFactor * Vector3.up);
// ...
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/362706.html
下一篇:FlowEnt補間庫漸變
