方法:
Quaternion ClampRotation(Quaternion q)
{q.x /= q.w; //q.x/q.w == tan(θ/2)
q.y /= q.w;
q.z /= q.w;
q.w = 1;
float angle = 2 * Mathf.Rad2Deg * Mathf.Atan(q.x); //Mathf.Atan(q.x)表示把数转换为弧度,Mathf.Rad2Deg 表示把弧度转化为角度
angle = Mathf.Clamp(angle, -90, 90);
q.x = Mathf.Tan(Mathf.Deg2Rad * (angle/2));//Mathf.Deg2Rad * (angle/2)表示把角度转化为弧度,Mathf.Tan()里面的数是弧度
return q;
}
主函数:
float xRot = Input.GetAxis("Mouse Y") * CameraSet.YSensitive;
float yRot = Input.GetAxis("Mouse X") * CameraSet.XSensitive;
m_camQuation *= Quaternion.Euler(-xRot, 0f, 0f);
m_camQuation = ClampRotation(m_camQuation);
m_camTrans.localRotation = m_camQuation;
m_chaQuation *= Quaternion.Euler(0f, yRot, 0f);
m_chaTrans.rotation = m_chaQuation;