/Scripts/MouseOrbitDrag.js

http://acid-and-base.googlecode.com/ · JavaScript · 59 lines · 43 code · 13 blank · 3 comment · 8 complexity · 257d9e35cbeb9315536636b0b4cd1ef1 MD5 · raw file

  1. var target : Transform;
  2. var distance = 10.0;
  3. var cameraSpeed = 5;
  4. var xSpeed = 175.0;
  5. var ySpeed = 75.0;
  6. var yMinLimit = 20; //Lowest vertical angle in respect with the target.
  7. var yMaxLimit = 80;
  8. var minDistance = 5; //Min distance of the camera from the target
  9. var maxDistance = 20;
  10. private var x = 0.0;
  11. private var y = 0.0;
  12. function Start () {
  13. var angles = transform.eulerAngles;
  14. x = angles.y;
  15. y = angles.x;
  16. // Make the rigid body not change rotation
  17. if (rigidbody)
  18. rigidbody.freezeRotation = true;
  19. }
  20. function LateUpdate () {
  21. if(Time.timeScale != 0)
  22. {
  23. if (target && camera) {
  24. //Zooming with mouse
  25. distance -= Input.GetAxis("Mouse ScrollWheel")*distance;
  26. distance = Mathf.Clamp(distance, minDistance, maxDistance);
  27. //Detect mouse drag;
  28. if(Input.GetMouseButton(0)) {
  29. x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
  30. y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
  31. }
  32. y = ClampAngle(y, yMinLimit, yMaxLimit);
  33. var rotation = Quaternion.Euler(y, x, 0);
  34. var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
  35. transform.position = Vector3.Lerp (transform.position, position, cameraSpeed*Time.deltaTime);
  36. transform.rotation = rotation;
  37. }
  38. }
  39. }
  40. static function ClampAngle (angle : float, min : float, max : float) {
  41. if (angle < -360)
  42. angle += 360;
  43. if (angle > 360)
  44. angle -= 360;
  45. return Mathf.Clamp (angle, min, max);
  46. }