using UnityEngine; using System.Collections; public class MyMouseOrbit : MonoBehaviour { public Transform target; public float distance; public float xSpeed; public float ySpeed; public int yMinLimit; public int yMaxLimit; private float x = 0.0f; private float y = 0.0f; public void Start () { var angles = transform.eulerAngles; x = angles.y; y = angles.x; // Make the rigid body not change rotation if (rigidbody) rigidbody.freezeRotation = true; } public void LateUpdate () { if (target) { x += Input.GetAxis("Mouse X") * xSpeed * 0.02f; y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f; y = ClampAngle(y, yMinLimit, yMaxLimit); MyQuaternion rotation = MyQuaternion.Euler(y, x, 0.0f); Vector3 position = rotation * new Vector3(0.0f, 0.0f, -distance) + target.position; transform.rotation.Set(rotation.X,rotation.Y,rotation.Z,rotation.W); transform.position = position; } } public static float ClampAngle (float angle,float min,float max) { if (angle < -360) angle += 360; if (angle > 360) angle -= 360; return Mathf.Clamp (angle, min, max); } }