Advertisement
Guest User

Hacho

a guest
May 27th, 2015
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var target : Transform;
  2. var distanceMin = 10.0;
  3. var distanceMax = 15.0;
  4. var distanceInitial = 12.5;
  5. var scrollSpeed = 1.0;
  6.  
  7. var xSpeed = 250.0;
  8. var ySpeed = 120.0;
  9.  
  10. var yMinLimit = -20;
  11. var yMaxLimit = 80;
  12.  
  13. private var x = 0.0;
  14. private var y = 0.0;
  15. private var distanceCurrent = 0.0;
  16.  
  17. @script AddComponentMenu ("Camera-Control/Key Mouse Orbit")
  18.  
  19. function Start () {
  20.     var angles = transform.eulerAngles;
  21.     x = angles.y;
  22.     y = angles.x;
  23.  
  24.     distanceCurrent = distanceInitial;
  25.  
  26.     // Make the rigid body not change rotation
  27.     if (rigidbody)
  28.         rigidbody.freezeRotation = true;
  29. }
  30.  
  31. function LateUpdate () {
  32.     if (target) {
  33.         x += Input.GetAxis("Horizontal") * xSpeed * 0.02;
  34.         y -= Input.GetAxis("Vertical") * ySpeed * 0.02;
  35.         distanceCurrent -= Input.GetAxis("Mouse ScrollWheel") * scrollSpeed;
  36.        
  37.         distanceCurrent = Mathf.Clamp(distanceCurrent, distanceMin, distanceMax);
  38.         y = ClampAngle(y, yMinLimit, yMaxLimit);
  39.                
  40.         var rotation = Quaternion.Euler(y, x, 0);
  41.         var position = rotation * Vector3(0.0, 0.0, -distanceCurrent) + target.position;
  42.        
  43.         transform.rotation = rotation;
  44.         transform.position = position;
  45.     }
  46. }
  47.  
  48. static function ClampAngle (angle : float, min : float, max : float) {
  49.     if (angle < -360)
  50.         angle += 360;
  51.     if (angle > 360)
  52.         angle -= 360;
  53.     return Mathf.Clamp (angle, min, max);
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement