Unity邏輯程式碼開發介紹_攝像機平滑旋轉並限定角度四元數方式
阿新 • • 發佈:2019-01-27
開發FPS的遊戲時,涉及的相機的平滑旋轉,下面是我採用四元數的方式,並且限定旋轉上下限:
public float XSensitivity = 2f;
public int MinimumX = -30;
public int MaximumX = 30;
private Quaternion m_CameraTargetRot;
void Update()
{
float xRot = Input.GetAxis("Mouse Y") * YSensitivity; //獲取滑鼠旋轉
m_CameraTargetRot *= Quaternion
m_CameraTargetRot = ClampRotationAroundXAxis(m_CameraTargetRot); //限定角度範圍
camera.localRotation = Quaternion.Slerp(camera.localRotation, m_CameraTargetRot,
smoothTime * Time.deltaTime); // 攝像機平滑插值
}
private Quaternion ClampRotationAroundXAxis
{
q.x /= q.w;
q.y /= q.w;
q.z /= q.w;
q.w = 1.0f;
float angleX = 2.0f * Mathf.Rad2Deg * Mathf.Atan(q.x);
angleX = Mathf.Clamp(angleX, MinimumX, MaximumX);
q.x = Mathf.Tan(0.5f * Mathf.Deg2Rad *
return q;
}
如有錯誤,大家可以一起交流
聯絡方式 qq: 940299880