当前位置: 代码迷 >> 综合 >> Unity3D 第一人称相机旋转限制技巧
  详细解决方案

Unity3D 第一人称相机旋转限制技巧

热度:95   发布时间:2023-10-28 07:28:35.0
    //当前相机x轴旋转度private float currentCameraRotationX = 0f;//相机旋转x 变化值private float cameraRotationX = 0f;//相机旋转限制度数private float cameraRotationLimit = 80f;if (cam != null){//设定旋转角度currentCameraRotationX -= cameraRotationX;currentCameraRotationX = Mathf.Clamp(currentCameraRotationX,-cameraRotationLimit,cameraRotationLimit);//赋值x旋转cam.transform.localEulerAngles = new Vector3(currentCameraRotationX,0f,0f);}

说明:

        上述是限制相机x轴旋转角度技巧的关键代码,第一部分 currentCameraRotationX记录当前旋转度数,cameraRotationX记录旋转的传入旋转变化值,cameraRotationLimit记录旋转的限制角度。

方法详解:

        当前旋转和传入旋转变化值进行相减操作(相加会反向,此处可以自行测试验证),然后将当前旋转的值进行Mathf.Clamp()操作,限制的参数是cameraRotationLimit。最后对相机cam进行本地角度赋值。