You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.1 KiB

  1. var target : Transform;
  2. var distance = 10.0;
  3. var xSpeed = 250.0;
  4. var ySpeed = 120.0;
  5. var yMinLimit = -20;
  6. var yMaxLimit = 80;
  7. private var x = 0.0;
  8. private var y = 0.0;
  9. @script AddComponentMenu("Camera-Control/Mouse Orbit")
  10. function Start () {
  11. var angles = transform.eulerAngles;
  12. x = angles.y;
  13. y = angles.x;
  14. // Make the rigid body not change rotation
  15. if (GetComponent.<Rigidbody>())
  16. GetComponent.<Rigidbody>().freezeRotation = true;
  17. }
  18. function LateUpdate () {
  19. if (target) {
  20. x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
  21. y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
  22. y = ClampAngle(y, yMinLimit, yMaxLimit);
  23. var rotation = Quaternion.Euler(y, x, 0);
  24. var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
  25. transform.rotation = rotation;
  26. transform.position = position;
  27. }
  28. }
  29. static function ClampAngle (angle : float, min : float, max : float) {
  30. if (angle < -360)
  31. angle += 360;
  32. if (angle > 360)
  33. angle -= 360;
  34. return Mathf.Clamp (angle, min, max);
  35. }