Advertisement
evelynshilosky

MouseMovement - Part 21

Oct 31st, 2023
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.23 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) { //To Lock MouseMovement a screen is open
  22.             float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
  23.             float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
  24.  
  25.             //control rotation around x axis (Look up and down)
  26.             xRotation -= mouseY;
  27.  
  28.             //we clamp the rotation so we cant Over-rotate (like in real life)
  29.             xRotation = Mathf.Clamp(xRotation, -90f, 90f);
  30.  
  31.             //control rotation around y axis (Look up and down)
  32.             YRotation += mouseX;
  33.  
  34.             //applying both rotations
  35.             transform.localRotation = Quaternion.Euler(xRotation, YRotation, 0f);
  36.         }
  37.     }
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement