Guest User

MouseLook.cs

a guest
Jul 5th, 2014
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. // original by asteins
  2. // adapted by @torahhorse
  3. // http://wiki.unity3d.com/index.php/SmoothMouseLook
  4.  
  5. // Instructions:
  6. // There should be one MouseLook script on the Player itself, and another on the camera
  7. // player's MouseLook should use MouseX, camera's MouseLook should use MouseY
  8.  
  9. using UnityEngine;
  10. using System.Collections;
  11. using System.Collections.Generic;
  12.  
  13. public class MouseLook : MonoBehaviour
  14. {
  15.  
  16. public enum RotationAxes { MouseX = 1, MouseY = 2 }
  17. public RotationAxes axes = RotationAxes.MouseX;
  18. public bool invertY = false;
  19.  
  20. public float sensitivityX = 10F;
  21. public float sensitivityY = 9F;
  22.  
  23. public float minimumX = -360F;
  24. public float maximumX = 360F;
  25.  
  26. public float minimumY = -85F;
  27. public float maximumY = 85F;
  28.  
  29. float rotationX = 0F;
  30. float rotationY = 0F;
  31.  
  32. private List<float> rotArrayX = new List<float>();
  33. float rotAverageX = 0F;
  34.  
  35. private List<float> rotArrayY = new List<float>();
  36. float rotAverageY = 0F;
  37.  
  38. public float framesOfSmoothing = 5;
  39.  
  40. Quaternion originalRotation;
  41.  
  42. void Start ()
  43. {
  44. if (rigidbody)
  45. {
  46. rigidbody.freezeRotation = true;
  47. }
  48.  
  49. originalRotation = transform.localRotation;
  50. }
  51.  
  52. void Update ()
  53. {
  54. if (axes == RotationAxes.MouseX)
  55. {
  56. rotAverageX = 0f;
  57.  
  58. rotationX += Input.GetAxis("Mouse X") * sensitivityX * Time.timeScale;
  59.  
  60. rotArrayX.Add(rotationX);
  61.  
  62. if (rotArrayX.Count >= framesOfSmoothing)
  63. {
  64. rotArrayX.RemoveAt(0);
  65. }
  66. for(int i = 0; i < rotArrayX.Count; i++)
  67. {
  68. rotAverageX += rotArrayX[i];
  69. }
  70. rotAverageX /= rotArrayX.Count;
  71. rotAverageX = ClampAngle(rotAverageX, minimumX, maximumX);
  72.  
  73. Quaternion xQuaternion = Quaternion.AngleAxis (rotAverageX, Vector3.up);
  74. transform.localRotation = originalRotation * xQuaternion;
  75. }
  76. else
  77. {
  78. rotAverageY = 0f;
  79.  
  80. float invertFlag = 1f;
  81. if( invertY )
  82. {
  83. invertFlag = -1f;
  84. }
  85. rotationY += Input.GetAxis("Mouse Y") * sensitivityY * invertFlag * Time.timeScale;
  86.  
  87. rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
  88.  
  89. rotArrayY.Add(rotationY);
  90.  
  91. if (rotArrayY.Count >= framesOfSmoothing)
  92. {
  93. rotArrayY.RemoveAt(0);
  94. }
  95. for(int j = 0; j < rotArrayY.Count; j++)
  96. {
  97. rotAverageY += rotArrayY[j];
  98. }
  99. rotAverageY /= rotArrayY.Count;
  100.  
  101. Quaternion yQuaternion = Quaternion.AngleAxis (rotAverageY, Vector3.left);
  102. transform.localRotation = originalRotation * yQuaternion;
  103. }
  104. }
  105.  
  106. public void SetSensitivity(float s)
  107. {
  108. sensitivityX = s;
  109. sensitivityY = s;
  110. }
  111.  
  112. public static float ClampAngle (float angle, float min, float max)
  113. {
  114. angle = angle % 360;
  115. if ((angle >= -360F) && (angle <= 360F)) {
  116. if (angle < -360F) {
  117. angle += 360F;
  118. }
  119. if (angle > 360F) {
  120. angle -= 360F;
  121. }
  122. }
  123. return Mathf.Clamp (angle, min, max);
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment