Skip to content

Instantly share code, notes, and snippets.

@augustoerico
Created June 17, 2018 19:20
Show Gist options
  • Save augustoerico/cf52b3468f0f40b3f015296ca38dc2aa to your computer and use it in GitHub Desktop.
Save augustoerico/cf52b3468f0f40b3f015296ca38dc2aa to your computer and use it in GitHub Desktop.
using UnityEngine;
public class OrbitalCamera : MonoBehaviour {
public GameObject lookAt;
public float followSpeed = 3.5f;
public float minAngle = -45.0f;
public float maxAngle = 60.0f;
private float distance = 0.0f;
private float lookH = 0.0f;
private float lookV = 0.0f;
void Start () {
distance = (transform.position - lookAt.transform.position).magnitude;
}
void Update () {
lookH = (lookH + Input.GetAxis ("Mouse X")) % 360;
lookV = Mathf.Clamp (lookV + Input.GetAxis ("Mouse Y") * (-1), minAngle, maxAngle);
}
void LateUpdate () {
Move ();
}
private void Move () {
Quaternion rotation = Quaternion.Euler (lookV, lookH, 0);
Vector3 nextPosition = lookAt.transform.position + rotation * new Vector3 (0, 0, - distance);
transform.position = Vector3.Lerp (
transform.position, nextPosition, followSpeed * Time.deltaTime
);
transform.LookAt (lookAt.transform.position);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment