Advertisement
evelynshilosky

MouseMovement - Part 1

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