Advertisement
evelynshilosky

MouseMovement - Part 27

Dec 12th, 2023
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.28 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class MouseMovement : MonoBehaviour
  6. {
  7.  
  8.     public float mouseSensitivity = 100f;
  9.  
  10.     float xRotation = 0f;
  11.     float YRotation = 0f;
  12.  
  13.     void Start()
  14.     {
  15.         //Locking the cursor to the middle of the screen and making it invisible
  16.         Cursor.lockState = CursorLockMode.Locked;
  17.     }
  18.  
  19.     void Update()
  20.     {
  21.         if (!InventorySystem.Instance.isOpen && !CraftingSystem.Instance.isOpen && !MenuManager.Instance.isMenuOpen && !DialogueSystem.Instance.dialogueUIActive)
  22.         { //To Lock MouseMovement a screen is open
  23.             float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
  24.             float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
  25.  
  26.             //control rotation around x axis (Look up and down)
  27.             xRotation -= mouseY;
  28.  
  29.             //we clamp the rotation so we cant Over-rotate (like in real life)
  30.             xRotation = Mathf.Clamp(xRotation, -90f, 90f);
  31.  
  32.             //control rotation around y axis (Look up and down)
  33.             YRotation += mouseX;
  34.  
  35.             //applying both rotations
  36.             transform.localRotation = Quaternion.Euler(xRotation, YRotation, 0f);
  37.         }
  38.     }
  39. }
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement