Advertisement
Guest User

Untitled

a guest
Oct 24th, 2015
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.45 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. // Very simple smooth mouselook modifier for the MainCamera in Unity
  4. // by Francis R. Griffiths-Keam - www.runningdimensions.com
  5.  
  6. [AddComponentMenu("Camera/Simple Smooth Mouse Look ")]
  7. public class Aim : MonoBehaviour
  8. {
  9.     Vector2 _mouseAbsolute;
  10.     Vector2 _smoothMouse;
  11.  
  12.     public Vector2 clampInDegrees = new Vector2(360, 180);
  13.     public bool lockCursor;
  14.     public Vector2 sensitivity = new Vector2(2, 2);
  15.     public Vector2 smoothing = new Vector2(3, 3);
  16.     public Vector2 targetDirection;
  17.     public Vector2 targetCharacterDirection;
  18.  
  19.     // Assign this if there's a parent object controlling motion, such as a Character Controller.
  20.     // Yaw rotation will affect this object instead of the camera if set.
  21.     public GameObject characterBody;
  22.  
  23.     void Start()
  24.     {
  25.         // Set target direction to the camera's initial orientation.
  26.         targetDirection = transform.localRotation.eulerAngles;
  27.  
  28.         // Set target direction for the character body to its inital state.
  29.         if (characterBody) targetCharacterDirection = characterBody.transform.localRotation.eulerAngles;
  30.     }
  31.  
  32.     void Update()
  33.     {
  34.         // Ensure the cursor is always locked when set
  35.         Screen.lockCursor = lockCursor;
  36.  
  37.         // Allow the script to clamp based on a desired target value.
  38.         var targetOrientation = Quaternion.Euler(targetDirection);
  39.         var targetCharacterOrientation = Quaternion.Euler(targetCharacterDirection);
  40.  
  41.         // Get raw mouse input for a cleaner reading on more sensitive mice.
  42.         var mouseDelta = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
  43.  
  44.         // Scale input against the sensitivity setting and multiply that against the smoothing value.
  45.         mouseDelta = Vector2.Scale(mouseDelta, new Vector2(sensitivity.x * smoothing.x, sensitivity.y * smoothing.y));
  46.  
  47.         // Interpolate mouse movement over time to apply smoothing delta.
  48.         _smoothMouse.x = Mathf.Lerp(_smoothMouse.x, mouseDelta.x, 1f / smoothing.x);
  49.         _smoothMouse.y = Mathf.Lerp(_smoothMouse.y, mouseDelta.y, 1f / smoothing.y);
  50.  
  51.         // Find the absolute mouse movement value from point zero.
  52.         _mouseAbsolute += _smoothMouse;
  53.  
  54.         // Clamp and apply the local x value first, so as not to be affected by world transforms.
  55.         if (clampInDegrees.x < 360)
  56.             _mouseAbsolute.x = Mathf.Clamp(_mouseAbsolute.x, -clampInDegrees.x * 0.5f, clampInDegrees.x * 0.5f);
  57.  
  58.         var xRotation = Quaternion.AngleAxis(-_mouseAbsolute.y, targetOrientation * Vector3.right);
  59.         transform.localRotation = xRotation;
  60.  
  61.         // Then clamp and apply the global y value.
  62.         if (clampInDegrees.y < 360)
  63.             _mouseAbsolute.y = Mathf.Clamp(_mouseAbsolute.y, -clampInDegrees.y * 0.5f, clampInDegrees.y * 0.5f);
  64.  
  65.         transform.localRotation *= targetOrientation;
  66.  
  67.         // If there's a character body that acts as a parent to the camera
  68.         if (characterBody)
  69.         {
  70.             var yRotation = Quaternion.AngleAxis(_mouseAbsolute.x, characterBody.transform.up);
  71.             characterBody.transform.localRotation = yRotation;
  72.             characterBody.transform.localRotation *= targetCharacterOrientation;
  73.         }
  74.         else
  75.         {
  76.             var yRotation = Quaternion.AngleAxis(_mouseAbsolute.x, transform.InverseTransformDirection(Vector3.up));
  77.             transform.localRotation *= yRotation;
  78.         }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement