Advertisement
Guest User

Untitled

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