Advertisement
Creator

MouseOrbit

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