Advertisement
evelynshilosky

MouseMovement - Part 7

Jun 2nd, 2024
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.14 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.  
  14.     void Start()
  15.     {
  16.  
  17.  
  18.       //Locking the cursor to the middle of the screen and making it invisible
  19.       Cursor.lockState = CursorLockMode.Locked;
  20.  
  21.  
  22.     }
  23.  
  24.  
  25.     void Update()
  26.     {
  27.  
  28.       if (!InventorySystem.Instance.isOpen && !CraftingSystem.Instance.isOpen) {
  29.  
  30.          float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
  31.          float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
  32.  
  33.           //control rotation around x axis (Look up and down)
  34.           xRotation -= mouseY;
  35.  
  36.           //we clamp the rotation so we cant Over-rotate (like in real life)
  37.           xRotation = Mathf.Clamp(xRotation, -90f, 90f);
  38.  
  39.           //control rotation around y axis (Look up and down)
  40.           YRotation += mouseX;
  41.  
  42.           //applying both rotations
  43.           transform.localRotation = Quaternion.Euler(xRotation, YRotation, 0f);
  44.        }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement