VitoPy

CameraRotation

Jun 11th, 2021 (edited)
119
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class MouseLook : MonoBehaviour
  6. {
  7.    public enum RotationAxes
  8.    {
  9.         MouseXAndY = 0,
  10.         MouseX = 1,
  11.         MouseY = 2
  12.    }
  13.    public RotationAxes axes = RotationAxes.MouseXAndY;
  14.    public float SensitivityHor = 9.0f;
  15.    public float SensitivityVer = 9.0f;
  16.  
  17.    public float minimumVert = -45.0f;
  18.    public float maximumVert = 45.0f;
  19.  
  20.    private float _rotationX = 0;
  21.  
  22.     void Start()
  23.     {
  24.         Rigidbody body = GetComponent<Rigidbody>();
  25.         if (body != null)
  26.         {
  27.             body.freezeRotation = true;
  28.         }
  29.     }
  30.  
  31.     // Update is called once per frame
  32.     void Update()
  33.     {
  34.         if (axes == RotationAxes.MouseX)
  35.         {
  36.             transform.Rotate(0, Input.GetAxis("Mouse X") * SensitivityHor,0);
  37.         }
  38.         else if (axes == RotationAxes.MouseY)
  39.         {
  40.             _rotationX -= Input.GetAxis("Mouse Y") * SensitivityVer;
  41.             _rotationX = Mathf.Clamp(_rotationX, minimumVert, maximumVert);
  42.             float rotationY = transform.localEulerAngles.y;
  43.             transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);
  44.         }
  45.         else
  46.         {
  47.             _rotationX -= Input.GetAxis("Mouse Y") * SensitivityVer;
  48.             _rotationX = Mathf.Clamp(_rotationX, minimumVert, maximumVert);
  49.  
  50.             float delta = Input.GetAxis("Mouse X") * SensitivityHor;
  51.             float rotationY = transform.localEulerAngles.y + delta;
  52.  
  53.             transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);
  54.         }
  55.     }
  56. }
  57.  
RAW Paste Data