Advertisement
evelynshilosky

MouseMovement - Part 1

Jan 21st, 2025
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.32 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.  
  13.     void Start()
  14.     {
  15.         Cursor.lockState = CursorLockMode.Locked;
  16.         playerMovement = GetComponent<PlayerMovement>();
  17.         UpdateCameraReference();
  18.     }
  19.  
  20.     void Update()
  21.     {
  22.         UpdateCameraReference();
  23.         HandleMouseMovement();
  24.     }
  25.  
  26.     void UpdateCameraReference()
  27.     {
  28.         currentCamera = playerMovement.isMale ? playerMovement.maleCamera : playerMovement.femaleCamera;
  29.     }
  30.  
  31.     void HandleMouseMovement()
  32.     {
  33.         float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
  34.         float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
  35.  
  36.         verticalRotation -= mouseY;
  37.         verticalRotation = Mathf.Clamp(verticalRotation, minVerticalRotation, maxVerticalRotation);
  38.  
  39.         currentCamera.localRotation = Quaternion.Euler(verticalRotation, 0f, 0f);
  40.         transform.Rotate(Vector3.up * mouseX);
  41.     }
  42.  
  43.     public float GetVerticalRotation()
  44.     {
  45.         return verticalRotation;
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement