Advertisement
Guest User

Smooth Mouse Look

a guest
Nov 8th, 2014
724
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. using System.Collections;
  4.  
  5. using System.Collections.Generic;
  6.  
  7.  
  8.  
  9. public class SmoothMouseLook : MonoBehaviour
  10.  
  11. {
  12.  
  13. /*
  14.  
  15. This script is used to average the mouse input over x
  16.  
  17. amount of frames in order to create a smooth mouselook.
  18.  
  19. */
  20.  
  21.  
  22.  
  23. //Mouse look sensitivity
  24.  
  25. public float sensitivityX = 2f;
  26.  
  27. public float sensitivityY = 2f;
  28.  
  29.  
  30.  
  31. //Default mouse sensitivity
  32.  
  33. public float defaultSensX = 2f;
  34.  
  35. public float defaultSensY = 2f;
  36.  
  37.  
  38.  
  39. //Minimum angle you can look up
  40.  
  41. public float minimumY = -60f;
  42.  
  43. public float maximumY = 60f;
  44.  
  45.  
  46.  
  47. //Number of frames to be averaged, used for smoothing mouselook
  48.  
  49. public int frameCounterX = 35;
  50.  
  51. public int frameCounterY = 35;
  52.  
  53.  
  54.  
  55. //Mouse rotation input
  56.  
  57. private float rotationX = 0f;
  58.  
  59. private float rotationY = 0f;
  60.  
  61.  
  62.  
  63. //Used to calculate the rotation of this object
  64.  
  65. private Quaternion xQuaternion;
  66.  
  67. private Quaternion yQuaternion;
  68.  
  69. private Quaternion originalRotation;
  70.  
  71.  
  72.  
  73. //Array of rotations to be averaged
  74.  
  75. private List<float> rotArrayX = new List<float> ();
  76.  
  77. private List<float> rotArrayY = new List<float> ();
  78.  
  79.  
  80.  
  81. void Start ()
  82.  
  83. {
  84.  
  85. //Lock/Hide cursor
  86.  
  87.  
  88.  
  89.  
  90.  
  91. if (rigidbody)
  92.  
  93. rigidbody.freezeRotation = true;
  94.  
  95.  
  96.  
  97. originalRotation = transform.localRotation;
  98.  
  99. }
  100.  
  101.  
  102.  
  103. void FixedUpdate ()
  104.  
  105. {
  106.  
  107.  
  108.  
  109. //Mouse/Camera Movement Smoothing:
  110.  
  111. //Average rotationX for smooth mouselook
  112.  
  113. float rotAverageX = 0f;
  114.  
  115. rotationX += Input.GetAxis ("Mouse X") * sensitivityX;
  116.  
  117.  
  118.  
  119. //Add the current rotation to the array, at the last position
  120.  
  121. rotArrayX.Add (rotationX);
  122.  
  123.  
  124.  
  125. //Reached max number of steps? Remove the oldest rotation from the array
  126.  
  127. if (rotArrayX.Count >= frameCounterX) {
  128.  
  129. rotArrayX.RemoveAt (0);
  130.  
  131. }
  132.  
  133.  
  134.  
  135. //Add all of these rotations together
  136.  
  137. for (int i_counterX = 0; i_counterX < rotArrayX.Count; i_counterX++) {
  138.  
  139. //Loop through the array
  140.  
  141. rotAverageX += rotArrayX[i_counterX];
  142.  
  143. }
  144.  
  145.  
  146.  
  147. //Now divide by the number of rotations by the number of elements to get the average
  148.  
  149. rotAverageX /= rotArrayX.Count;
  150.  
  151.  
  152.  
  153. //Average rotationY, same process as above
  154.  
  155. float rotAverageY = 0;
  156.  
  157. rotationY += Input.GetAxis ("Mouse Y") * sensitivityY;
  158.  
  159. rotationY = ClampAngle (rotationY, minimumY, maximumY);
  160.  
  161. rotArrayY.Add (rotationY);
  162.  
  163.  
  164.  
  165. if (rotArrayY.Count >= frameCounterY) {
  166.  
  167. rotArrayY.RemoveAt (0);
  168.  
  169. }
  170.  
  171.  
  172.  
  173. for (int i_counterY = 0; i_counterY < rotArrayY.Count; i_counterY++) {
  174.  
  175. rotAverageY += rotArrayY[i_counterY];
  176.  
  177. }
  178.  
  179.  
  180.  
  181. rotAverageY /= rotArrayY.Count;
  182.  
  183.  
  184.  
  185. //Apply and rotate this object
  186.  
  187. xQuaternion = Quaternion.AngleAxis (rotAverageX, Vector3.up);
  188.  
  189. yQuaternion = Quaternion.AngleAxis (rotAverageY, Vector3.left);
  190.  
  191. transform.localRotation = originalRotation * xQuaternion * yQuaternion;
  192.  
  193. }
  194.  
  195.  
  196.  
  197.  
  198.  
  199. private float ClampAngle (float angle, float min, float max)
  200.  
  201. {
  202.  
  203. if (angle < -360f)
  204.  
  205. angle += 360f;
  206.  
  207. if (angle > 360f)
  208.  
  209. angle -= 360f;
  210.  
  211.  
  212.  
  213. return Mathf.Clamp (angle, min, max);
  214.  
  215. }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement