evelynshilosky

MouseMovement - Part 6.1

Mar 27th, 2025
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.67 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class MouseMovement : MonoBehaviour
  4. {
  5.     public float mouseSensitivity = 100f;
  6.     public float minVerticalRotation = -90f;
  7.     public float maxVerticalRotation = 90f;
  8.  
  9.     private float verticalRotation = 0f;
  10.     private PlayerMovement playerMovement;
  11.     private Transform currentCamera;
  12.     private bool controlsEnabled = true;
  13.  
  14.     void Start()
  15.     {
  16.         Cursor.lockState = CursorLockMode.Locked;
  17.         playerMovement = GetComponent<PlayerMovement>();
  18.         UpdateCameraReference();
  19.     }
  20.  
  21.     void Update()
  22.     {
  23.         if (controlsEnabled && !UIManager.Instance.isInventoryOpen)
  24.         {
  25.             UpdateCameraReference();
  26.             HandleMouseMovement();
  27.         }
  28.     }
  29.  
  30.     void UpdateCameraReference()
  31.     {
  32.         currentCamera = playerMovement.isMale ? playerMovement.maleCamera : playerMovement.femaleCamera;
  33.     }
  34.  
  35.     void HandleMouseMovement()
  36.     {
  37.         float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
  38.         float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
  39.  
  40.         verticalRotation -= mouseY;
  41.         verticalRotation = Mathf.Clamp(verticalRotation, minVerticalRotation, maxVerticalRotation);
  42.  
  43.         currentCamera.localRotation = Quaternion.Euler(verticalRotation, 0f, 0f);
  44.         transform.Rotate(Vector3.up * mouseX);
  45.     }
  46.  
  47.     public float GetVerticalRotation()
  48.     {
  49.         return verticalRotation;
  50.     }
  51.  
  52.     public void SetControlsEnabled(bool enabled)
  53.     {
  54.         controlsEnabled = enabled;
  55.         Cursor.lockState = enabled ? CursorLockMode.Locked : CursorLockMode.None;
  56.         Cursor.visible = !enabled;
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment