Advertisement
JustPrototype

Unity3D MouseLook

Jan 14th, 2019
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.68 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3. /// <summary>
  4. /// Add this to the Camera
  5. /// </summary>
  6. [AddComponentMenu("Camera-Control/Mouse Look")]
  7. public class MouseLook : MonoBehaviour
  8. {
  9.     public MouseLook.RotationAxes axes;
  10.     public float sensitivityX = 15f;
  11.     public float sensitivityY = 15f;
  12.     public float minimumX = -360f;
  13.     public float maximumX = 360f;
  14.     public float minimumY = -60f;
  15.     public float maximumY = 60f;
  16.     private float rotationY;
  17.  
  18.     public enum RotationAxes
  19.     {
  20.         MouseXAndY,
  21.         MouseX,
  22.         MouseY
  23.     }
  24.  
  25.     private void Update()
  26.     {
  27.         if (this.axes == MouseLook.RotationAxes.MouseXAndY)
  28.         {
  29.             float y = base.transform.localEulerAngles.y + Input.GetAxis("Mouse X") * this.sensitivityX;
  30.             this.rotationY += Input.GetAxis("Mouse Y") * this.sensitivityY;
  31.             this.rotationY = Mathf.Clamp(this.rotationY, this.minimumY, this.maximumY);
  32.             base.transform.localEulerAngles = new Vector3(-this.rotationY, y, 0f);
  33.         }
  34.         else if (this.axes == MouseLook.RotationAxes.MouseX)
  35.         {
  36.             base.transform.Rotate(0f, Input.GetAxis("Mouse X") * this.sensitivityX, 0f);
  37.         }
  38.         else
  39.         {
  40.             this.rotationY += Input.GetAxis("Mouse Y") * this.sensitivityY;
  41.             this.rotationY = Mathf.Clamp(this.rotationY, this.minimumY, this.maximumY);
  42.             base.transform.localEulerAngles = new Vector3(-this.rotationY, base.transform.localEulerAngles.y, 0f);
  43.         }
  44.     }
  45.  
  46.     private void Start()
  47.     {
  48.         if (base.GetComponent<Rigidbody>())
  49.         {
  50.             base.GetComponent<Rigidbody>().freezeRotation = true;
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement