Advertisement
Guest User

Untitled

a guest
May 27th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 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.  
  11. private var x = 0.0;
  12. private var y = 0.0;
  13.  
  14.  
  15. @script AddComponentMenu("Camera-Control/Mouse Orbit")
  16.  
  17. function Start () {
  18. var angles = transform.eulerAngles;
  19. x = angles.y;
  20. y = angles.x;
  21.  
  22. // Make the rigid body not change rotation
  23. if (GetComponent.<Rigidbody>())
  24. GetComponent.<Rigidbody>().freezeRotation = true;
  25. }
  26.  
  27. function Update(){
  28.  
  29. //Updating camera distance on every frame
  30. distance = Raycast3.distance3;
  31.  
  32. //Setting maximum distance so the camera doesnt go too far
  33. if(distance > 2){
  34. distance = 2;
  35. }
  36.  
  37.  
  38.  
  39. //Make the camera rotate not only with the mouse, but also with Arrow Keys/WASD
  40. if(( Input.GetKey(KeyCode.A)) || ( Input.GetKey(KeyCode.LeftArrow))) {
  41. x -= 3; //The higher the number the faster the camera rotation
  42. }
  43. if(( Input.GetKey(KeyCode.D)) || ( Input.GetKey(KeyCode.RightArrow))) {
  44. x += 3; //The higher the number the faster the camera rotation
  45. }
  46. }
  47. function LateUpdate () {
  48. if (target) {
  49. x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
  50. y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
  51.  
  52. y = ClampAngle(y, yMinLimit, yMaxLimit);
  53.  
  54. var rotation = Quaternion.Euler(y, x, 0);
  55. var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
  56.  
  57. transform.rotation = rotation;
  58. transform.position = position;
  59. }
  60. }
  61.  
  62. static function ClampAngle (angle : float, min : float, max : float) {
  63. if (angle < -360)
  64. angle += 360;
  65. if (angle > 360)
  66. angle -= 360;
  67. return Mathf.Clamp (angle, min, max);
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement