Guest User

Untitled

a guest
May 21st, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. var target : Transform;
  2.  
  3. var targetHeight = 2.0;
  4. var distance = 5.0;
  5.  
  6. var maxDistance = 20;
  7. var minDistance = 2.5;
  8.  
  9. var xSpeed = 250.0;
  10. var ySpeed = 120.0;
  11.  
  12. var yMinLimit = -20;
  13. var yMaxLimit = 80;
  14.  
  15. var zoomRate = 20;
  16.  
  17. var rotationDampening = 3.0;
  18.  
  19. private var x = 0.0;
  20. private var y = 0.0;
  21.  
  22. @script AddComponentMenu("Camera-Control/WoW Camera")
  23.  
  24. function Start () {
  25. var angles = transform.eulerAngles;
  26. x = angles.y;
  27. y = angles.x;
  28.  
  29. // Make the rigid body not change rotation
  30. if (rigidbody)
  31. rigidbody.freezeRotation = true;
  32. }
  33.  
  34. function LateUpdate () {
  35. if(!target)
  36. return;
  37.  
  38. // If either mouse buttons are down, let them govern camera position
  39. if (Input.GetMouseButton(0) || Input.GetMouseButton(1))
  40. {
  41. x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
  42. y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
  43.  
  44. // otherwise, ease behind the target if any of the directional keys are pressed
  45. } else if(Input.GetAxis("Vertical") || Input.GetAxis("Horizontal")) {
  46. var targetRotationAngle = target.eulerAngles.y;
  47. var currentRotationAngle = transform.eulerAngles.y;
  48. x = Mathf.LerpAngle(currentRotationAngle, targetRotationAngle, rotationDampening * Time.deltaTime);
  49. }
  50.  
  51. distance -= (Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime) * zoomRate * Mathf.Abs(distance);
  52. distance = Mathf.Clamp(distance, minDistance, maxDistance);
  53.  
  54. y = ClampAngle(y, yMinLimit, yMaxLimit);
  55.  
  56. var rotation:Quaternion = Quaternion.Euler(y, x, 0);
  57. var position = target.position - (rotation * Vector3.forward * distance + Vector3(0,-targetHeight,0));
  58.  
  59. transform.rotation = rotation;
  60. transform.position = position;
  61. }
  62.  
  63. static function ClampAngle (angle : float, min : float, max : float) {
  64. if (angle < -360)
  65. angle += 360;
  66. if (angle > 360)
  67. angle -= 360;
  68. return Mathf.Clamp (angle, min, max);
  69. }
Add Comment
Please, Sign In to add comment