using UnityEngine; using System.Collections; public class OrbitCamera : MonoBehaviour { // the target to follow (orbit around) [SerializeField] private Transform target; // the rotation speed for the camera public float rotationSpeed = 1.5f; private float rotY; // current rotation of Y public Vector3 offset = new Vector3(0, 2.5f, -3.5f); // the distance at which to follow the player // Use this for initialization void Start () { rotY = transform.eulerAngles.y; } // LateUpdate is called after all other Update methods void LateUpdate () { // Get the input on the horizontal axis float horInput = Input.GetAxis("Horizontal"); if (horInput != 0) // keyboard input along horizontal axis { // Rotate the camera slowly using the keyboard horizontal input (arrow keys ...) rotY += horInput * rotationSpeed; } else { // Rotate the camera quickly with the mouse rotY += Input.GetAxis("Mouse X") * rotationSpeed * 3; } // Euler angles are limited by a phenomenon called "gimbal lock", which prevents them from measuring orientation when the pitch angle approaches +/- 90 degrees. // Euler angles will always fail to produce reliable estimates when the pitch angle approaches 90 degrees. Quaternion rotation = Quaternion.Euler(0, rotY, 0); // update the camera's offset position according to the camera's rotation transform.position = target.position + (rotation * offset); // face the target transform.LookAt(target); } }