Advertisement
TheDenVxUA

Untitled

Jun 18th, 2020
1,275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.81 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3. using UnityStandardAssets.CrossPlatformInput;
  4.  
  5. namespace UnityStandardAssets.Characters.FirstPerson
  6. {
  7.     [Serializable]
  8.     public class MouseLook
  9.     {
  10.         public float XSensitivity = 2f;
  11.         public float YSensitivity = 2f;
  12.         public bool clampVerticalRotation = true;
  13.         public float MinimumX = -90F;
  14.         public float MaximumX = 90F;
  15.         public bool smooth;
  16.         public float smoothTime = 5f;
  17.         public bool lockCursor = true;
  18.  
  19.         float OffsetX = 0;
  20.         float OffsetY = 0;
  21.  
  22.         private Quaternion m_CharacterTargetRot;
  23.         private Quaternion m_CameraTargetRot;
  24.         private bool m_cursorIsLocked = true;
  25.  
  26.         public void Init(Transform character, Transform camera)
  27.         {
  28.             m_CharacterTargetRot = character.localRotation;
  29.             m_CameraTargetRot = camera.localRotation;
  30.             OffsetY = 0.4f;
  31.         }
  32.  
  33.         public void ChangeOffset(float x, float y)
  34.         {
  35.             OffsetX = x;
  36.             OffsetY = y;
  37.         }
  38.  
  39.         public void LookRotation(Transform character, Transform camera)
  40.         {
  41.             //DON't CHANGE!
  42.             float yRot = CrossPlatformInputManager.GetAxis("Mouse X") * XSensitivity + OffsetX;
  43.             float xRot = CrossPlatformInputManager.GetAxis("Mouse Y") * YSensitivity + OffsetY;
  44.  
  45.             OffsetY = 0;
  46.             OffsetX = 0;
  47.  
  48.             m_CharacterTargetRot *= Quaternion.Euler (0f, yRot, 0f);
  49.             m_CameraTargetRot *= Quaternion.Euler (-xRot, 0f, 0f);
  50.  
  51.             if(clampVerticalRotation)
  52.                 m_CameraTargetRot = ClampRotationAroundXAxis (m_CameraTargetRot);
  53.  
  54.             if(smooth)
  55.             {
  56.                 character.localRotation = Quaternion.Slerp (character.localRotation, m_CharacterTargetRot,
  57.                     smoothTime * Time.deltaTime);
  58.                 camera.localRotation = Quaternion.Slerp (camera.localRotation, m_CameraTargetRot,
  59.                     smoothTime * Time.deltaTime);
  60.             }
  61.             else
  62.             {
  63.                 character.localRotation = m_CharacterTargetRot;
  64.                 camera.localRotation = m_CameraTargetRot;
  65.             }
  66.  
  67.             UpdateCursorLock();
  68.         }
  69.  
  70.         public void SetCursorLock(bool value)
  71.         {
  72.             lockCursor = value;
  73.             if(!lockCursor)
  74.             {//we force unlock the cursor if the user disable the cursor locking helper
  75.                 Cursor.lockState = CursorLockMode.None;
  76.                 Cursor.visible = true;
  77.             }
  78.         }
  79.  
  80.         public void UpdateCursorLock()
  81.         {
  82.             //if the user set "lockCursor" we check & properly lock the cursos
  83.             if (lockCursor)
  84.                 InternalLockUpdate();
  85.         }
  86.  
  87.         private void InternalLockUpdate()
  88.         {
  89.             if(Input.GetKeyUp(KeyCode.Escape))
  90.             {
  91.                 m_cursorIsLocked = false;
  92.             }
  93.             else if(Input.GetMouseButtonUp(0))
  94.             {
  95.                 m_cursorIsLocked = true;
  96.             }
  97.  
  98.             if (m_cursorIsLocked)
  99.             {
  100.                 Cursor.lockState = CursorLockMode.Locked;
  101.                 Cursor.visible = false;
  102.             }
  103.             else if (!m_cursorIsLocked)
  104.             {
  105.                 Cursor.lockState = CursorLockMode.None;
  106.                 Cursor.visible = true;
  107.             }
  108.         }
  109.  
  110.         Quaternion ClampRotationAroundXAxis(Quaternion q)
  111.         {
  112.             q.x /= q.w;
  113.             q.y /= q.w;
  114.             q.z /= q.w;
  115.             q.w = 1.0f;
  116.  
  117.             float angleX = 2.0f * Mathf.Rad2Deg * Mathf.Atan (q.x);
  118.  
  119.             angleX = Mathf.Clamp (angleX, MinimumX, MaximumX);
  120.  
  121.             q.x = Mathf.Tan (0.5f * Mathf.Deg2Rad * angleX);
  122.  
  123.             return q;
  124.         }
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement