自己编写第一人称视角过程中的摄像机角度的旋转:
如果使用欧拉角进行控制,会产生万象锁(自行百度),因此,用四元数,代码如下:
Quaternion ViewControll(Quaternion view)
{
view.x /= view.w;
view.y /= view.w;
view.z /= view.w;
view.w = 1;
float angle = 2 * Mathf.Rad2Deg * Mathf.Atan(view.x);
angle = Mathf.Clamp(angle, 90, -90);
view.x = Mathf.Tan((Mathf.Deg2Rad) * (angle / 2));
return view;
}
主函数中:
float yRot = Input.GetAxis("Mouse X") * CameraSet.XSensitive;
//m_camRotate += new Vector3(-xRot, 0f, 0f);
m_camRotate *= Quaternion.Euler(-xRot, 0f, 0f);
m_camRotate = ViewControll(m_camRotate);
m_camTrans.localRotation = m_camRotate;
完整的代码: