Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. [AddComponentMenu("Camera-Control/Mouse Orbit with zoom")]
  5. public class MouseOrbitImproved : MonoBehaviour {
  6.  
  7. public Transform target;
  8. public float distance = 5.0f;
  9. public float xSpeed = 120.0f;
  10. public float ySpeed = 120.0f;
  11.  
  12. public float yMinLimit = -20f;
  13. public float yMaxLimit = 80f;
  14.  
  15. public float distanceMin = .5f;
  16. public float distanceMax = 15f;
  17.  
  18. private Rigidbody rigidbody;
  19.  
  20. float x = 0.0f;
  21. float y = 0.0f;
  22.  
  23. // Use this for initialization
  24. void Start ()
  25. {
  26. Vector3 angles = transform.eulerAngles;
  27. x = angles.y;
  28. y = angles.x;
  29.  
  30. rigidbody = GetComponent<Rigidbody>();
  31.  
  32. // Make the rigid body not change rotation
  33. if (rigidbody != null)
  34. {
  35. rigidbody.freezeRotation = true;
  36. }
  37. }
  38.  
  39. void LateUpdate ()
  40. {
  41. if (target)
  42. {
  43. x += Input.GetAxis("Mouse X") * xSpeed * distance * 0.02f;
  44. y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
  45.  
  46. y = ClampAngle(y, yMinLimit, yMaxLimit);
  47.  
  48. Quaternion rotation = Quaternion.Euler(y, x, 0);
  49.  
  50. distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel")*5, distanceMin, distanceMax);
  51.  
  52. RaycastHit hit;
  53. if (Physics.Linecast (target.position, transform.position, out hit))
  54. {
  55. distance -= hit.distance;
  56. }
  57. Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
  58. Vector3 position = rotation * negDistance + target.position;
  59.  
  60. transform.rotation = rotation;
  61. transform.position = position;
  62. }
  63. }
  64.  
  65. public static float ClampAngle(float angle, float min, float max)
  66. {
  67. if (angle < -360F)
  68. angle += 360F;
  69. if (angle > 360F)
  70. angle -= 360F;
  71. return Mathf.Clamp(angle, min, max);
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement