Advertisement
_EagleOwle_

Clamp angle

Dec 3rd, 2018
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.33 KB | None | 0 0
  1. private void ClampAngle()
  2.     {
  3.         if (Cursor.visible == true)
  4.             return;
  5.  
  6.         Quaternion yQuaternion = Quaternion.AngleAxis(Input.GetAxis("Mouse Y") * sensitivity * Time.deltaTime, -Vector3.right);
  7.         Quaternion xQuaternion = Quaternion.AngleAxis(Input.GetAxis("Mouse X") * sensitivity * Time.deltaTime, Vector3.up);
  8.  
  9.         //Проверяем угол между условным "центром" и нужным вращением
  10.         if (Quaternion.Angle(transform.rotation, cameraTransform.localRotation * xQuaternion) < 15)
  11.         {
  12.             if (Quaternion.Angle(transform.rotation, cameraTransform.localRotation * yQuaternion) < 15)
  13.             {
  14.                 cameraTransform.localRotation *= xQuaternion * yQuaternion;
  15.             }
  16.             else
  17.             {
  18.                 cameraTransform.localRotation *= xQuaternion;
  19.             }
  20.         }
  21.         else
  22.         {
  23.             if (Quaternion.Angle(transform.rotation, cameraTransform.localRotation * yQuaternion) < 15)
  24.             {
  25.                 cameraTransform.localRotation *= yQuaternion;
  26.             }
  27.             else
  28.             {
  29.                 return;
  30.             }
  31.         }
  32.  
  33.         cameraTransform.localEulerAngles = new Vector3(cameraTransform.localEulerAngles.x, cameraTransform.localEulerAngles.y, 0);
  34.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement