evelynshilosky

MouseMovement

Apr 26th, 2024
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.41 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class MouseMovement : MonoBehaviour
  6. {
  7.     public float mouseSensitivity = 100f;
  8.  
  9.     float xRotation = 0f;
  10.     float YRotation = 0f;
  11.  
  12.     void Start()
  13.     {
  14.         //Locking the cursor to the middle of the screen and making it invisible
  15.         Cursor.lockState = CursorLockMode.Locked;
  16.     }
  17.  
  18.  
  19.     void Update()
  20.     {
  21.         if (!InventorySystem.Instance.isOpen && !CraftingSystem.Instance.isOpen && !MenuManager.Instance.isMenuOpen && !DialogueSystem.Instance.dialogueUIActive && !QuestManager.Instance.isQuestMenuOpen && !StorageManager.Instance.storageUIOpen && !CampfireUIManager.Instance.isUIOpen)
  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.  
  36.             //applying both rotations
  37.             transform.localRotation = Quaternion.Euler(xRotation, YRotation, 0f);
  38.         }
  39.  
  40.     }
  41. }
  42.  
Add Comment
Please, Sign In to add comment